Skip to content

Commit e75b7cd

Browse files
Reject sentinels as types when defined within a class nested in a function
Signed-off-by: Edgar Ramírez Mondragón <edgarrm358@gmail.com>
1 parent 057504d commit e75b7cd

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

mypy/semanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3402,7 +3402,7 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
34023402

34033403
def is_sentinel_declaration(self, s: AssignmentStmt) -> bool:
34043404
"""Does this assignment define a PEP 661 sentinel singleton?"""
3405-
if self.is_func_scope() or s.unanalyzed_type is not None:
3405+
if self.is_nested_within_func_scope() or s.unanalyzed_type is not None:
34063406
return False
34073407
if len(s.lvalues) != 1 or not isinstance(s.lvalues[0], NameExpr):
34083408
return False

test-data/unit/check-sentinels.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ def outer() -> None:
6565
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
6666
[builtins fixtures/tuple.pyi]
6767

68+
[case testSentinelClassNestedInFunctionNotTypeExpression]
69+
from typing_extensions import Sentinel
70+
71+
def outer() -> None:
72+
class C:
73+
MISSING = Sentinel("MISSING")
74+
75+
def inner(x: C.MISSING) -> None: ... # E: Variable "__main__.C@4.MISSING" is not valid as a type \
76+
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
77+
[builtins fixtures/tuple.pyi]
78+
6879
[case testSentinelImplicitlyFinal]
6980
from typing_extensions import Sentinel
7081

0 commit comments

Comments
 (0)