-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add typing._UnionSpecialForm and add it to isinstance/issubclass
#7508
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
Conversation
It's been sleepy lately; looks like it's woken up now though |
typing.UnionSpecialForm and add it to isinstance/issubclasstyping._UnionSpecialForm and add it to isinstance/issubclass
This comment has been minimized.
This comment has been minimized.
|
@KotlinIsland, have you tested locally to check that this solves the issue? |
|
Not at all. Whats the quickest way to do this? |
|
Diff from mypy_primer, showing the effect of this PR on open source code: steam.py (https://github.com/Gobot1234/steam.py)
- steam/ext/commands/converters.py:507: error: Incompatible types in assignment (expression has type "Union[Any, object]", variable has type "Union[T, str, Tuple[T, ...], Tuple[str, ...], Union[ConverterBase[Any], BasicConverter[Any]]]") [assignment]
+ steam/ext/commands/converters.py:507: error: Incompatible types in assignment (expression has type "Union[Any, T, str, _UnionSpecialForm]", variable has type "Union[T, str, Tuple[T, ...], Tuple[str, ...], Union[ConverterBase[Any], BasicConverter[Any]]]") [assignment]
|
Run
|
|
Okay, just tested it and it works fine in the negative and positive. |
|
The primer output indicates that mypy has no problems with this. I'm actually a little surprised at how blasé the CI is about this change. @erictraut / @rchen152: would this change break pyright or pytype at all? |
|
It might break pyright (I'd have to confirm), but it would be an easy fix to make. T his particular fix doesn't look correct to me though. This appears to be a bug in mypy, not in typeshed. Shouldn't mypy just fix its handling here? I'll also point out that I don't understand why someone would want to use the old So this change seems both unnecessary and in the wrong place. |
I can't find a realistic use case either. Some uses of the But This works (which surprised me the other day): But as Eric said, why would you do that if you can use |
|
Because it reflects the runtime behavior. How is that not reason enough?
@erictraut Could you please elaborate on this, I can't see any way that this could possibly be a bug in mypy.
It's situations like this that make me so confused at the decisions made in Python, if there is no use case for this functionality, then why was it ever added to the implementation of |
|
Think about all of the other places in the type system where unions are accepted. Typeshed stubs don't need to specify "a union can be used here" because type checkers automatically handle unions as a special case.
This was added in Python 3.10 to support the new syntax in PEP 604. At runtime, |
We should reflect the runtime behaviour as closely as possible at all times (in my opinion). But we also shouldn't recklessly make changes that might break large chunks of the typing ecosystem, if they don't have a good use case. I'm personally fine if the consensus here is just "mypy should add some more special-casing". I agree that we should generally stick to the runtime implementation as closely as possible, and this PR would take us further away from that. |
|
Related: python/mypy/issues/12370 |
pytype should be unaffected, since it has its own copies of the builtins and typing stubs. |
|
I don't think there's enough consensus to make this change, so I'm closing this. But thank you for the PR; I thought it was an interesting and creative idea. |
isinstanceis currently not accepting instances of_UnionGenericAliasakatyping.Union.This change introduces a sussy fake impostor type used as a marker for these values.
Resolves #7505