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

Error handling for recursive TypeVar defaults (PEP 696) #16925

Merged
merged 3 commits into from
Feb 20, 2024

Conversation

cdce8p
Copy link
Collaborator

@cdce8p cdce8p commented Feb 16, 2024

This PR adds some additional error handling for recursive TypeVar defaults.
Open issue for future PRs:

  • Expanding nested recursive defaults, e.g. T2 = list[T1 = str]
  • Scope binding, especially for TypeAliasTypes
  • Figure out if visit_type_alias_type for the TypeVarDefaultTranslator visitor is actually needed. The base class does not provide a default implementation. So far I haven't hit the NotImplementedError.

Ref: #14851

@cdce8p cdce8p added the topic-pep-696 TypeVar defaults label Feb 16, 2024

This comment has been minimized.

@JelleZijlstra
Copy link
Member

Seems like psycopg found the NotImplementedError for you... It's a TypeAlias generic over a TypeVar with both a bound and a default:

https://github.com/psycopg/psycopg/blob/ef6941df5b94997f79b429347c5d9b84e600bdd3/psycopg_pool/psycopg_pool/abc.py#L24

This comment has been minimized.

@cdce8p
Copy link
Collaborator Author

cdce8p commented Feb 16, 2024

Seems like psycopg found the NotImplementedError for you... It's a TypeAlias generic over a TypeVar with both a bound and a default:

Yeah. It's really helpful to get that feedback immediately. Anyway, it was a simple fix. The TypeAliasType is already evaluated earlier, so there is no need to travers it. Just returning the type unchanged is enough.

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Thanks! Noticed a typo; I'll fix that and merge.

mypy/semanal.py Outdated Show resolved Hide resolved
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@JelleZijlstra JelleZijlstra merged commit 790e8a7 into python:master Feb 20, 2024
18 checks passed
@cdce8p cdce8p deleted the TypeVar-12-error-handling branch February 20, 2024 00:09
hamdanal pushed a commit to hamdanal/mypy that referenced this pull request Feb 20, 2024
This PR adds some additional error handling for recursive TypeVar
defaults.
Open issue for future PRs:
- Expanding nested recursive defaults, e.g. `T2 = list[T1 = str]`
- Scope binding, especially for TypeAliasTypes

Ref: python#14851
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-pep-696 TypeVar defaults
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants