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

Using TypeAlias containing TypeVar resulting in unknown in TypeAlias #6715

Closed
dephiros opened this issue Dec 12, 2023 · 1 comment
Closed
Labels
as designed Not a bug, working as intended bug Something isn't working

Comments

@dephiros
Copy link

Describe the bug
When using TypeAlias that includes a TypeVar, the TypeVar results in unknown type
Using the TypeVar directly does not result in the error

Code or Screenshots

This is a simplified example from django-stubs _DisplayT type that produces the same error

from typing import TypeVar, Callable, TypeAlias

_ModelT = TypeVar("_ModelT", bound=str)
_DisplayT: TypeAlias = str | Callable[[_ModelT], str | bool]


def generic_func(arg: _ModelT, arg1: _ModelT) -> _DisplayT:
    return arg


def generic_func1(arg: _ModelT, arg1: _ModelT) -> str | Callable[[_ModelT], str | bool]:
    return arg
Screenshot 2023-12-12 at 08 23 12

VS Code extension or command-line
pyright: 1.1.339

@dephiros dephiros added the bug Something isn't working label Dec 12, 2023
@erictraut
Copy link
Collaborator

Pyright's behavior is correct here, so this isn't a bug.

You're creating a generic type alias that has one type parameter. That means any time you use the type alias, you should provide a single type argument. If you don't provide a type argument, type checkers will assume that you mean Any. This is the same as with generic types. For example, if you use list in a type annotation, a static type checker will assume that you mean list[Any].

You can fix the bug in your code by replacing _DisplayT with _DisplayT[_ModelT] in the generic_func function signature.

If you enable the reportMissingTypeArgument diagnostic rule in pyright, it will tell you when you have omitted a type argument for a generic class or type alias.

If you're using Python 3.12, you can switch to the new type parameter syntax (defined in PEP 695), which is less confusing than the older syntax. Here's how your code looks with the new syntax:

from typing import Callable

type Display[T: str] = str | Callable[[T], str | bool]

def generic_func[T: str](arg: T, arg1: T) -> Display[T]:
    return arg

@erictraut erictraut added the as designed Not a bug, working as intended label Dec 12, 2023
@erictraut erictraut closed this as not planned Won't fix, can't repro, duplicate, stale Dec 12, 2023
dephiros added a commit to dephiros/django-stubs that referenced this issue Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as designed Not a bug, working as intended bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants