KEMBAR78
fix: fail check if not enough or too many types provided to `TypeAlia… by bzoracler · Pull Request #18308 · python/mypy · GitHub
Skip to content

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: ...

@github-actions

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.

@github-actions
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