Skip to content

assigning a TypeVar to an identical value is reported as an error. #12528

@mrolle45

Description

@mrolle45

In my project, I have multiple definitions of the same TypeVar. They originated from merging (with pytype) the original modules with their generated stub files. It may be that pytype was in error, but I'll look into that later.
In this example, the TypeVar Self appears several times in the stub files. The result is:

A.py:
from B import *
Self = TypeVar('Self')

B.py:
Self = TypeVar('Self')

mypy reports the redefinition of Self in A as an error.

I expect that a type checker should consider this as a benign redefinition. It would ignore it, and replace the existing symbol table node (called B.Self) with a new node called A.Self. On any subsequent reanalysis of A, I believe that mypy would treat this as already analyzed and do nothing.

You might disagree with me about this. Does the PEP have any specification about this?

In semanal.py line 3157 is the fail message. I would say that the existing node should first be compared with the new name, values, and bound for equality. When they are equal, skip the subsequent fail message and the return of False.

Your Environment

  • Mypy version used: 0.931
  • Python version used: 3.7

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions