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

fix: fail check if not enough or too many types provided to `TypeAlia… #18308

Merged
merged 2 commits into from
Dec 19, 2024

Conversation

bzoracler
Copy link
Contributor

…sType()`

Fixes #18307 by failing a type alias call check if the number of positional arguments isn't exactly 2 (one for the type name as a literal string, one for the target type to alias).

Before:

from typing_extensions import TypeAliasType

T1 = TypeAliasType("T1", int, str)  # Silently passes and uses `int` as the target type, should be an error
T2 = TypeAliasType("T2")  # Crashes

After:

T1 = TypeAliasType("T1", int, str)  # E: Too many positional arguments for "TypeAliasType"
T2 = TypeAliasType("T2")  # E: Missing positional argument "value" in call to "TypeAliasType"

The error messages above are propagated from a check with the TypeAliasType constructor definition from the stubs, so no further special-casing is necessary:

    class TypeAliasType:
        def __init__(
            self, name: str, value: Any, *, type_params: tuple[TypeVar | ParamSpec | TypeVarTuple, ...] = ()
        ) -> None: ...

This comment has been minimized.

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, could you add a test that exercises this code path?

To find the place to add a test, grep for TypeAliasType among the .test files.

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 1f9317f into python:master Dec 19, 2024
19 checks passed
@bzoracler bzoracler deleted the fix/typealias-num-args branch December 19, 2024 21:01
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.

Incompletely defining a TypeAliasType causes mypy to crash
2 participants