Skip to content

New analyser: Complain about type variable redefinition only after we decided it is valid #7093

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

Merged
merged 1 commit into from
Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions mypy/newsemanal/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2686,17 +2686,7 @@ def process_typevar_declaration(self, s: AssignmentStmt) -> bool:
self.fail("Cannot declare the type of a type variable", s)
return False

assert isinstance(s.rvalue, CallExpr)
name = lvalue.name
names = self.current_symbol_table()
existing = names.get(name)
if existing and not (isinstance(existing.node, PlaceholderNode) or
# Also give error for another type variable with the same name.
(isinstance(existing.node, TypeVarExpr) and
existing.node is s.rvalue.analyzed)):
self.fail("Cannot redefine '%s' as a type variable" % name, s)
return False

if not self.check_typevar_name(call, name, s):
return False

Expand All @@ -2713,6 +2703,14 @@ def process_typevar_declaration(self, s: AssignmentStmt) -> bool:
return False
variance, upper_bound = res

existing = self.current_symbol_table().get(name)
if existing and not (isinstance(existing.node, PlaceholderNode) or
# Also give error for another type variable with the same name.
(isinstance(existing.node, TypeVarExpr) and
existing.node is call.analyzed)):
self.fail("Cannot redefine '%s' as a type variable" % name, s)
return False

if self.options.disallow_any_unimported:
for idx, constraint in enumerate(values, start=1):
if has_any_from_unimported_type(constraint):
Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/check-newsemanal.test
Original file line number Diff line number Diff line change
Expand Up @@ -2774,3 +2774,14 @@ def g() -> None:
nonlocal bar
bar = [] # type: typing.List[int] # E: Name 'bar' already defined on line 11
[builtins fixtures/list.pyi]

[case testNewAnalyzerMoreInvalidTypeVarArgumentsDeferred]
from typing import TypeVar, Generic

defer: Yes

S = TypeVar('S', covariant=True, contravariant=True) # E: TypeVar cannot be both covariant and contravariant \
# E: "int" not callable

class Yes: ...
[builtins fixtures/bool.pyi]