Skip to content

Allow type aliases in runtime context #8779

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

Merged
merged 2 commits into from
May 5, 2020

Conversation

ilevkivskyi
Copy link
Member

Fixes #5354

This PR allows using type aliases in runtime context and instead gives the an object type (which is the best type that we know). The new error messages are less specific, but the old logic caused annoying false positives for various ORMs that use non-annotation syntax, and for runtime type validation tools.

While working on this I found a minor bug in object_or_any_from_type() that returned Any for Union[int, Any] while it should be object.

@ilevkivskyi ilevkivskyi requested a review from JukkaL May 5, 2020 09:24
@JukkaL
Copy link
Collaborator

JukkaL commented May 5, 2020

I like this! Now inferred types of runtime types are inconsistent, however:

from typing import Union

A = Union[int, str]

reveal_type(A)  # object
reveal_type(Union[int, str])  # Any

Can you also update the type of Union[int, str] (and other similar) to be object?

@ilevkivskyi
Copy link
Member Author

@JukkaL opened python/typeshed#3966 for this.

@ilevkivskyi ilevkivskyi merged commit 5c67598 into python:master May 5, 2020
@ilevkivskyi ilevkivskyi deleted the allow-alias-runtime branch May 5, 2020 12:43
ilevkivskyi added a commit to python/typeshed that referenced this pull request May 5, 2020
This is an accompanying PR for python/mypy#8779, see python/mypy#8779 (comment)

I also noticed that Python 2 and Python 3 versions are a bit out of sync, so I also put them back in sync.
@Tinche
Copy link
Contributor

Tinche commented May 5, 2020

Thank you!

vishalkuo pushed a commit to vishalkuo/typeshed that referenced this pull request Jun 26, 2020
This is an accompanying PR for python/mypy#8779, see python/mypy#8779 (comment)

I also noticed that Python 2 and Python 3 versions are a bit out of sync, so I also put them back in sync.
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.

type alias to union is invalid in runtime context
3 participants