-
Notifications
You must be signed in to change notification settings - Fork 234
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
Reason given for disallowing non-concrete subtype assignment is unsound #1647
Comments
Thanks for posting this. I suggest posting this to the discuss, which should attract more attention. |
FYI discuss is here: https://discuss.python.org/ |
I think Also see cases like python/mypy#16919 / python/mypy#16890 I'm curious what the real world code where you're encountering issues looks like? |
Please check this issue: python/mypy#4717 Any function that accepts a type object as its argument and returns an instance of that type gets affected by the concreteness restriction. In my experience, DI containers really struggle with this because they are supposed to facilitate binding of implementation to abstraction: |
Thanks for the links, both of those look like cases of |
I think
|
python/mypy#9773 can somewhat improve the situation, but not sure DI containers would want to accept any Probably the But, anyway, when the main reason for having concreteness restriction is unsound, why do we keep it at all? |
@ippeiukai I believe Eric brought up your issue here in case you wanted to participate! |
@NeilGirdhar Thanks! |
Issue
According to the discussion #1305, “type checkers allow incompatible
__init__
overrides, because flagging them would be too disruptive.”Accepting above as de facto, the example given as the main reason for disallowing non-concrete subtype assignment in the following section of the spec is unsound:
https://github.com/python/typing/blob/e08290b70f58df509f998cbbe09a8e65abb57a9b/docs/spec/protocol.rst#type-and-class-objects-vs-protocols
(credit python/mypy#4717 (comment) for pointing out the contradiction)
Thoughts
One radical approach would be to remove the concreteness rule from the spec altogether. The type of
Proto
istype[Proto]
, and if the constructor compatibility is not checked, there is little reason to disallow it from being assigned to variables annotated astype[Proto]
.If that is too radical, the spec can stay as is but the reasoning and associated examples should be clearly marked ‘historical’ and no longer valid. That way, we can avoid any immediate changes in practice, but at the same time encourage discussions towards appropriate future specs.
The text was updated successfully, but these errors were encountered: