Disallow no-args generic aliases when using PEP 613 explicit aliases by brianschubert · Pull Request #18173 · python/mypy (original) (raw)

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 andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation6 Commits4 Checks18 Files changed

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

brianschubert

Per the type system conformance tests, this is ok:

ListAlias = list x = ListAliasint # OK

While this is not:

ListAlias: TypeAlias = list x: ListAlias[int] # E: already specialized

Mypy currently permits both. This PR makes mypy reject the latter case, improving conformance.

As part of this, no-args PEP 613 explicit aliases are no longer eagerly expanded.

(Also removed a stale comment referencing TypeAliasExpr.no_args, which was removed in #15924)

@brianschubert

@brianschubert

@brianschubert

Conformance results diff for this PR (aliases_explicit now passes):

diff --git a/conformance/results/mypy/aliases_explicit.toml b/conformance/results/mypy/aliases_explicit.toml index ee05762..f07e7ad 100644 --- a/conformance/results/mypy/aliases_explicit.toml +++ b/conformance/results/mypy/aliases_explicit.toml @@ -24,11 +24,11 @@ aliases_explicit.py:89: error: Invalid type: try using Literal[1] instead? [val aliases_explicit.py:90: error: Invalid type alias: expression is not a valid type [valid-type] aliases_explicit.py:90: error: Function "list" could always be true in boolean context [truthy-function] aliases_explicit.py:91: error: Invalid type alias: expression is not a valid type [valid-type] +aliases_explicit.py💯 error: Bad number of arguments for type alias, expected 0, given 1 [type-arg] aliases_explicit.py:101: error: "UnionType[list[Any], set[Any]]" not callable [operator] aliases_explicit.py:102: error: Bad number of arguments for type alias, expected 0, given 1 [type-arg] """ -conformance_automated = "Fail" +conformance_automated = "Pass" errors_diff = """ -Line 100: Expected 1 errors """ ignore_errors = ["Function "list" could always be true in boolean context"] diff --git a/conformance/results/mypy/version.toml b/conformance/results/mypy/version.toml index 699a899..1d9b711 100644 --- a/conformance/results/mypy/version.toml +++ b/conformance/results/mypy/version.toml @@ -1,2 +1,2 @@ -version = "mypy 1.14.0+dev.21587f01045246a9ecb54a054a5ba03e20cbbd19" -test_duration = 4.5 +version = "mypy 1.14.0+dev.6563c362fd5ee8e1fbf17775eab5dcad5a0a7459" +test_duration = 5.1

@github-actions GitHub Actions

This comment has been minimized.

@brianschubert

@github-actions GitHub Actions

This comment has been minimized.

@brianschubert

The psycopg hits are true positives from aliases of the form _GQueue: TypeAlias = queue.Queue inside if TYPE_CHECKING blocks. Pyright already rejects these. It should be simple for them to migrate these to explicit generic aliases a la _GQueue: TypeAlias = queue.Queue[T].

JukkaL

@brianschubert

@github-actions GitHub Actions

Diff from mypy_primer, showing the effect of this PR on open source code:

psycopg (https://github.com/psycopg/psycopg)

JukkaL

hauntsaninja added a commit to hauntsaninja/mypy that referenced this pull request

Jan 23, 2025

@hauntsaninja

hauntsaninja added a commit that referenced this pull request

Jan 23, 2025

@hauntsaninja

This is a partial revert of #18173 to unblock the 1.15 release

Fixes #18488

wesleywright pushed a commit that referenced this pull request

Jan 23, 2025

@hauntsaninja @wesleywright

This is a partial revert of #18173 to unblock the 1.15 release

Fixes #18488

x612skm pushed a commit to x612skm/mypy-dev that referenced this pull request

Feb 24, 2025

@hauntsaninja @x612skm

2 participants

@brianschubert @JukkaL