Skip to content

Allow PEP 604 type aliases to be used in runtime context. #11650

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

Closed
wants to merge 4 commits into from

Conversation

esoma
Copy link
Contributor

@esoma esoma commented Dec 1, 2021

I ramble about this here #11648 😄

This only solves the main issue that I had with #11648 (that a metaclass __or__ return type was lost in mypy when giving it a name).


This does not address the issue of union type aliases not being use-able with isinstance:

class A: ...
class B: ...
C = A | B
reveal_type(C) # Revealed type is "builtins.object"
isinstance(object(), C) # error: Parameterized generics cannot be used with class or instance checks
                        # error: Argument 2 to "isinstance" has incompatible type "object"; expected "Union[type, UnionType, Tuple[Union[type, UnionType, Tuple[Any, ...]], ...]]"

But it does get rid of the second error regarding argument 2 not being compatible and correctly reveals C as types.UnionType.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Thank you for your work! 🎉

I have left several questions 🙂


class F(metaclass=Meta): pass

reveal_type(F | F) # N: Revealed type is "types.UnionType"
Copy link
Member

Choose a reason for hiding this comment

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

I haven't personally used this trick yet, so I am thinking 🤔
Should not this be just F? Or do we want this special treatment of types.UnionType?

Copy link
Contributor Author

@esoma esoma Dec 3, 2021

Choose a reason for hiding this comment

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

I think there is confusion over Union and UnionType here (and imo, it is confusing and that's because unions are treated so specially). UnionType is the runtime type of a Union object:

X = str | int
print(type(X)) # <class 'types.UnionType'>

So, the type of X is always UnionType (it's worth noting that in previous version of mypy this was just builtins.object).


This is perhaps easier to grasp given:

class A: ...
reveal_type(A | A)  # types.UnionType
a: A | A
reveal_type(a) # Union[test2.A, test2.A]

Now, the second case not reducing is almost certainly a bug, but I think that is unrelated to this specific issue (it's also present in master) since it doesn't matter if its a TypeAlias or not (just if it was a PEP 604 union or not).

@brent-moffit
Copy link

It's been a while now, but this PR would still be useful. In the meantime I've worked around this using cast:

class A: ...
class B: ...
C = A | B
isinstance(object(), cast(UnionType, C))

@hauntsaninja
Copy link
Collaborator

I think these all work on master with sufficiently new Python

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.

4 participants