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

Type Alias inside class unsupported #10788

Closed
nipunn1313 opened this issue Jul 9, 2021 · 6 comments
Closed

Type Alias inside class unsupported #10788

nipunn1313 opened this issue Jul 9, 2021 · 6 comments
Labels
bug mypy got something wrong

Comments

@nipunn1313
Copy link
Contributor

nipunn1313 commented Jul 9, 2021

Bug Report

When there's a type alias inside a class, it's not considered to be a type by mypy.
pyright appears to support this.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.10&gist=509f8043f236ccc7e178dd56f526eaf1

import typing

T = typing.NewType('T', int)
class C:
    V = T
x: T = T(5)
y: C.V = C.V(5)

Expected Behavior

No errors

Actual Behavior

main.py:7: error: Variable "main.C.V" is not valid as a type
main.py:7: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
Found 1 error in 1 file (checked 1 source file)
@nipunn1313
Copy link
Contributor Author

nipunn1313 commented Oct 30, 2021

I found that

import typing

T = typing.NewType('T', int)
class C:
    V1 = T
    V2: typing.TypeAlias = T
x: T = T(5)
y1: C.V1 = C.V1(5)
y2: C.V2 = C.V2(5)

I found that y2 works but y1 doesn't. This is consistent with this comment - https://mail.python.org/archives/list/[email protected]/message/QNJEVUIHGCO6V4RYKBQK7FSRYHZZGWNH/ - on PEP613

@hauntsaninja
Copy link
Collaborator

Yeah, I think this is by design and PEP 613 is the appropriate way forward, see

# Fourth rule (special case): Non-subscripted right hand side creates a variable

@nipunn1313
Copy link
Contributor Author

PEP613 appears to explicitly ban type aliases in nested scope (https://www.python.org/dev/peps/pep-0613/#scope-restrictions)

Though the wording around it makes it seem to me that it meant to declare that implicit type aliases were banned in nested scope - and explicit type aliases are to be preferred (AKA V: TypeAlias = T). If this is the case, then perhaps we should modify the wording on PEP613 in the scope restriction section.

@hauntsaninja
Copy link
Collaborator

Oh interesting. I think mypy's current behaviour is good and don't see a reason explicit type aliases in nested scope should be banned by PEP. Going through mypy issues today it seems like this is not an uncommon use case.

I'd be happy with "only use explicit type aliases in nested scope" as PEP guidance. I guess pyre or whatever type checker can choose not to support type aliases in nested scope, but I don't think PEP should prohibit mypy from recognising this. Maybe worth emailing typing-sig about?

@erictraut
Copy link

FWIW, I interpreted the PEP to mean that an explicit type alias wasn't allowed in a nested scope, so pyright generates an error for this condition. Conversely, I allow implicit type aliases in nested scopes, since PEP 484 doesn't appear to disallow this. So if we want to clarify in the PEP that explicit type aliases are allowed in nested scopes, I'd be happy to make that change in pyright's logic.

@nipunn1313
Copy link
Contributor Author

IMO, explicit type alias / explicit variable should be allowed at any scope

V: TypeAlias = T  # Alias
V: Type[T] = T  # Variable

For the implicit ones - some ideas

  1. Could leave it up to the type checker.
  2. Could make it explicit in the PEP613
    a) global scope -> Alias, nested scope -> variable
    b) Always treat as Alias (any scope)

IMO, 2b makes the most sense to me (pyright's current behavior). It seems really rare to me that a type gets assigned to a variable other than an alias. 2a is closer to mypy's current behavior.

(1) allows for flexibility of either, which might be a good starting point. I'll try to email typing-sig! Thanks for commenting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants