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

mypy does not recognize tuple unpacking syntax for TypeVars. #11655

Open
realharry opened this issue Dec 3, 2021 · 5 comments
Open

mypy does not recognize tuple unpacking syntax for TypeVars. #11655

realharry opened this issue Dec 3, 2021 · 5 comments
Labels

Comments

@realharry
Copy link

Bug Report

This is really a minor issue, but if you declare multiple TypeVar variables in one line, mypy does not seem to recognize them as type variables. For example,

from typing import TypeVar, Generic
T1, T2 = TypeVar('T1'), TypeVar('T2')

class Container(Generic[T1]):
    class Item(Generic[T2]):
        pass

To Reproduce

  1. Declare more than one TypeVars in one line. (See the example code above.)
  2. Run mypy check on this script.

Expected Behavior

The T1 and T2 in the example above should be recognized as type variables.
They should pass mypy check.

Actual Behavior

Errors:

$ mypy typevar.py 
typevar.py:5: error: Free type variable expected in Generic[...]
typevar.py:6: error: Free type variable expected in Generic[...]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10.0
  • Operating system and version: Ubuntu 20.04
@realharry realharry added the bug mypy got something wrong label Dec 3, 2021
@realharry
Copy link
Author

It is really not a big issue, but I'm curious as to why it is not working. mypy uses a different parser than cpython? or, am i using mypy incorrectly?

@JelleZijlstra
Copy link
Member

Mypy recognizes specific syntactic patterns as TypeVar definitions, and this isn't one of them. I suppose we could add support, but I'm not sure that would be an improvement.

@realharry
Copy link
Author

thanks for the reply. i'm relatively new to using mypy, but my impression (so far) is that you do not have to learn a new (mypy-specific) syntax. everything you write is just a plain python (to me). now, this particular example shows that mypy requires a special syntax, and some valid python statements may not work with mypy. if that's the case, then there might be more exceptions. no? for me, T1, T2 = TypeVar('T1'), TypeVar('T2') is a valid python statement, and my expectation is that it should work with mypy as well. and, I want to emphasize that it is "my" expectation. different people may disagree.

@hauntsaninja
Copy link
Collaborator

mypy doesn't actually run any of your code (which is a feature! code can do all kinds of nasty things), and so will not always understand your code perfectly.
For an example, mypy will not complain about eval("1+'asdf'").
For an example more closely related to your original example, mypy will not understand e.g. globals()["T" + str(2)] = getattr(typing, "TypeVar")(chr(ord('S')+1) + '1').

I'd accept a PR that either provides a clearer error or accepts the multiple definition (look in semanal.py).

@sobolevn
Copy link
Member

sobolevn commented Dec 3, 2021

There are quite a lot of places where we require strict X = Some() syntax. This is how TypedDict(), namedtuple(), NamedTuple(), and possibly others are required to look.

I think that we can actually change all of these places. Because, using X, Y = Some(), 2 is not really common (and in my taste not very readable), but valid in runtime.

@AlexWaygood AlexWaygood added topic-runtime-semantics mypy doesn't model runtime semantics correctly priority-2-low feature and removed bug mypy got something wrong labels Mar 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants