Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve __new__ and __init__ precedence #18093

Closed
wants to merge 1 commit into from

Conversation

hauntsaninja
Copy link
Collaborator

@hauntsaninja hauntsaninja commented Nov 3, 2024

Fixes #5647

See also #5642

This PR is incomplete as it currently stands. We need to do something cleverer that handles the full constructor chain. See also: https://typing.readthedocs.io/en/latest/spec/constructors.html

Probably shouldn't merge, but maybe if we chose to tie break with __new__ only if the return type of __new__ is not a subtype of the type being constructed, this would unambiguously be a step in the right direction.

@hauntsaninja hauntsaninja force-pushed the init-new branch 2 times, most recently from 5a40f11 to ee93b0b Compare November 3, 2024 06:53
@hauntsaninja hauntsaninja marked this pull request as draft November 3, 2024 06:58

This comment has been minimized.

Copy link
Contributor

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

prefect (https://github.com/PrefectHQ/prefect)
- 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/results.py:942: error: Argument 1 has incompatible type "R@create_result"; expected "R@BaseResult"  [arg-type]
- src/prefect/results.py:1253: note: "_cache_object" of "BaseResult" defined here
- src/prefect/results.py:1449: error: Unexpected keyword argument "storage_block" for "_cache_object" of "BaseResult"  [call-arg]
- src/prefect/results.py:1449: error: Unexpected keyword argument "serializer" for "_cache_object" of "BaseResult"  [call-arg]
+ src/prefect/records/result_store.py:65: error: Unexpected keyword argument "_sync" for "write" of "PersistedResult"  [call-arg]
+ src/prefect/records/result_store.py:65: error: Value of type "Coroutine[Any, Any, R?]" must be used  [unused-coroutine]
+ src/prefect/records/result_store.py:65: note: Are you missing an await?
- src/prefect/logging/formatters.py:65: error: Argument 1 to "dumps" of "Serializer" has incompatible type "dict[str, Any]"; expected "D"  [arg-type]

rclip (https://github.com/yurijmikhalevich/rclip)
+ rclip/main.py:118: error: Need type annotation for "pbar"  [var-annotated]

beartype (https://github.com/beartype/beartype)
+ beartype/_check/error/errget.py:581: error: Unused "type: ignore" comment  [unused-ignore]

scrapy (https://github.com/scrapy/scrapy)
+ scrapy/utils/datatypes.py:94: error: Unused "type: ignore" comment  [unused-ignore]

mongo-python-driver (https://github.com/mongodb/mongo-python-driver)
+ bson/dbref.py:98: error: Need type annotation for "doc"  [var-annotated]

@hauntsaninja hauntsaninja changed the title Fix __new__ and __init__ precedence Improve __new__ and __init__ precedence Dec 27, 2024
@hauntsaninja hauntsaninja deleted the init-new branch December 28, 2024 01:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prefer __new__ over __init__ for determining callable type of a class
1 participant