Skip to content

Fix constructor type for subclasses of Any - #19295

Merged
ilevkivskyi merged 2 commits into
python:masterfrom
ilevkivskyi:correct-constructor-any
Jun 18, 2025
Merged

ilevkivskyi merged 2 commits into
python:masterfrom
ilevkivskyi:correct-constructor-any

Conversation

@ilevkivskyi

@ilevkivskyi ilevkivskyi commented Jun 14, 2025 •

Copy link
Copy Markdown
Member

Fixes #9815
Fixes #10848
Fixes #17781

Also discovered this while working on checkmember stuff. Previously, return type of the type object was the class where __init__() was defined (if there was an Any somewhere in MRO). And since we use return type for attribute access on type objects, it went completely sideways. Fix is simple (we accidentally used info instead of def_info in one place).

@ilevkivskyi

Copy link
Copy Markdown
Member Author

The fix here is quite trivial, so I will probably not wait for a review unless there are objections. cc @JukkaL

@github-actions

This comment has been minimized.

def __init__(self) -> None: ...
class C(B): ...

reveal_type(C) # N: Revealed type is "def () -> __main__.C"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about also testing a generic class that subclasses Any?

@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

poetry (https://github.com/python-poetry/poetry)
- src/poetry/console/commands/self/show/__init__.py:41: error: Returning Any from function declared to return "set[NormalizedName]"  [no-any-return]

prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/logging/loggers.py:144: error: Unused "type: ignore" comment  [unused-ignore]
- src/prefect/utilities/hashing.py:74: error: Argument 1 to "dumps" of "Serializer" has incompatible type "tuple[tuple[Any, ...], dict[str, Any]]"; expected "D"  [arg-type]
- src/prefect/context.py:829: error: Item "None" of "Optional[ContextModel]" has no attribute "current_tags"  [union-attr]
- src/prefect/tasks.py:930: error: Item "None" of "Optional[ContextModel]" has no attribute "current_tags"  [union-attr]
- src/prefect/tasks.py:1047: error: Item "None" of "Optional[ContextModel]" has no attribute "current_tags"  [union-attr]
+ src/prefect/tasks.py:1297: error: Argument 1 to "submit" of "TaskRunner" has incompatible type "Union[Task[P, R], Task[P, Coroutine[Any, Any, R]]]"; expected "Task[P, Coroutine[Any, Any, R]]"  [arg-type]
+ src/prefect/tasks.py:1297: error: Argument 3 to "submit" of "TaskRunner" has incompatible type "Optional[Union[Union[PrefectFuture[Any], Any], Iterable[Union[PrefectFuture[Any], Any]]]]"; expected "Optional[Iterable[PrefectFuture[Any]]]"  [arg-type]
- src/prefect/flow_engine.py:521: error: Item "None" of "Optional[ContextModel]" has no attribute "current_tags"  [union-attr]
- src/prefect/flow_engine.py:1084: error: Item "None" of "Optional[ContextModel]" has no attribute "current_tags"  [union-attr]
+ src/prefect/task_engine.py:985: error: Item "None" of "Optional[FlowRun]" has no attribute "run_count"  [union-attr]
- src/prefect/workers/base.py:838: error: Item "None" of "Optional[ContextModel]" has no attribute "current_tags"  [union-attr]
+ src/prefect/runtime/flow_run.py:140: error: Item "None" of "Optional[FlowRun]" has no attribute "id"  [union-attr]
+ src/prefect/runtime/flow_run.py:159: error: Item "None" of "Optional[FlowRun]" has no attribute "tags"  [union-attr]
+ src/prefect/runtime/flow_run.py:174: error: Item "None" of "Optional[FlowRun]" has no attribute "run_count"  [union-attr]
+ src/prefect/runtime/flow_run.py:189: error: Item "None" of "Optional[FlowRun]" has no attribute "name"  [union-attr]
+ src/prefect/runtime/flow_run.py:204: error: Item "None" of "Optional[Flow[Any, Any]]" has no attribute "name"  [union-attr]
+ src/prefect/runtime/flow_run.py:219: error: Item "None" of "Optional[Flow[Any, Any]]" has no attribute "version"  [union-attr]
+ src/prefect/runtime/flow_run.py:234: error: Item "None" of "Optional[FlowRun]" has no attribute "expected_start_time"  [union-attr]
+ src/prefect/runtime/flow_run.py:242: error: Incompatible return value type (got "Optional[dict[str, Any]]", expected "dict[str, Any]")  [return-value]
+ src/prefect/runtime/flow_run.py:257: error: Item "None" of "Optional[FlowRun]" has no attribute "parent_task_run_id"  [union-attr]
+ src/prefect/runtime/flow_run.py:334: error: Item "None" of "Optional[FlowRun]" has no attribute "job_variables"  [union-attr]
+ src/prefect/flow_runs.py:279: error: Item "None" of "Optional[FlowRun]" has no attribute "id"  [union-attr]
+ src/prefect/flow_runs.py:315: error: Item "None" of "Optional[FlowRun]" has no attribute "id"  [union-attr]
+ src/prefect/deployments/flow_runs.py:154: error: Item "None" of "Optional[FlowRun]" has no attribute "id"  [union-attr]
- src/prefect/deployments/flow_runs.py:156: error: Item "None" of "Optional[RunContext]" has no attribute "task_run"  [union-attr]
+ src/prefect/deployments/flow_runs.py:156: error: Item "None" of "Optional[TaskRunContext]" has no attribute "task_run"  [union-attr]
- src/prefect/deployments/flow_runs.py:161: error: Item "None" of "Optional[RunContext]" has no attribute "task_run"  [union-attr]
+ src/prefect/deployments/flow_runs.py:161: error: Item "None" of "Optional[TaskRunContext]" has no attribute "task_run"  [union-attr]

kornia (https://github.com/kornia/kornia)
- kornia/augmentation/container/ops.py:496: error: Unused "type: ignore" comment  [unused-ignore]

zulip (https://github.com/zulip/zulip)
+ zerver/tests/test_auth_backends.py:469: error: Cannot assign to a method  [method-assign]

@ilevkivskyi
ilevkivskyi merged commit d52ce3b into python:master Jun 18, 2025
@ilevkivskyi
ilevkivskyi deleted the correct-constructor-any branch June 18, 2025 23:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants