File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -3360,8 +3360,6 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
33603360 s .is_alias_def = False
33613361
33623362 sentinel_definition = self .is_sentinel_declaration (s )
3363- if sentinel_definition and self .is_existing_final_lvalue (s ):
3364- sentinel_definition = False
33653363
33663364 # OK, this is a regular assignment, perform the necessary analysis steps.
33673365 s .is_final_def = self .unwrap_final (s )
@@ -3399,15 +3397,6 @@ def is_sentinel_declaration(self, s: AssignmentStmt) -> bool:
33993397 return False
34003398 return True
34013399
3402- def is_existing_final_lvalue (self , s : AssignmentStmt ) -> bool :
3403- if len (s .lvalues ) != 1 or not isinstance (s .lvalues [0 ], NameExpr ):
3404- return False
3405- name = s .lvalues [0 ].name
3406- existing = self .current_symbol_table ().get (name )
3407- if existing is not None and is_final_node (existing .node ):
3408- return True
3409- return self .is_alias_for_final_name (name )
3410-
34113400 def setup_sentinel_var (self , s : AssignmentStmt ) -> None :
34123401 lvalue = s .lvalues [0 ]
34133402 assert isinstance (lvalue , NameExpr )
Original file line number Diff line number Diff line change 55import re
66from unittest import TestCase , skipUnless
77
8+ from librt .internal import ReadBuffer , WriteBuffer
9+
10+ from mypy .cache import read_tag
811from mypy .erasetype import erase_type , remove_instance_last_known_values
912from mypy .indirection import TypeIndirectionVisitor
1013from mypy .join import join_types
3336 AnyType ,
3437 CallableType ,
3538 Instance ,
39+ LITERAL_TYPE ,
3640 LiteralType ,
3741 NoneType ,
3842 Overloaded ,
@@ -75,6 +79,16 @@ def test_sentinel_literal_json_roundtrip(self) -> None:
7579 self .assertEqual (roundtrip .value , literal .value )
7680 self .assertEqual (roundtrip .fallback .type_ref , self .fx .a .type .fullname )
7781
82+ def test_sentinel_literal_ff_roundtrip (self ) -> None :
83+ literal = LiteralType (SentinelValue ("__main__.MISSING" , "MISSING" ), self .fx .a )
84+ data = WriteBuffer ()
85+ literal .write (data )
86+ buffer = ReadBuffer (data .getvalue ())
87+ assert read_tag (buffer ) == LITERAL_TYPE
88+ roundtrip = LiteralType .read (buffer )
89+ self .assertEqual (roundtrip .value , literal .value )
90+ self .assertEqual (roundtrip .fallback .type_ref , self .fx .a .type .fullname )
91+
7892 def test_simple_unbound_type (self ) -> None :
7993 u = UnboundType ("Foo" )
8094 assert_equal (str (u ), "Foo?" )
Original file line number Diff line number Diff line change @@ -3319,8 +3319,8 @@ def write(self, data: WriteBuffer) -> None:
33193319 self .fallback .write (data )
33203320 if isinstance (self .value , SentinelValue ):
33213321 write_tag (data , LITERAL_SENTINEL )
3322- write_str (data , self .value .fullname )
3323- write_str (data , self .value .name )
3322+ write_str_bare (data , self .value .fullname )
3323+ write_str_bare (data , self .value .name )
33243324 else :
33253325 write_literal (data , self .value )
33263326 write_tag (data , END_TAG )
@@ -3331,7 +3331,7 @@ def read(cls, data: ReadBuffer) -> LiteralType:
33313331 fallback = Instance .read (data )
33323332 tag = read_tag (data )
33333333 if tag == LITERAL_SENTINEL :
3334- value = SentinelValue (read_str (data ), read_str (data ))
3334+ value = SentinelValue (read_str_bare (data ), read_str_bare (data ))
33353335 else :
33363336 value = read_literal (data , tag )
33373337 ret = LiteralType (value , fallback )
Original file line number Diff line number Diff line change @@ -69,7 +69,18 @@ def outer() -> None:
6969from typing_extensions import Sentinel
7070
7171MISSING = Sentinel("MISSING")
72- MISSING = Sentinel("OTHER") # E: Cannot assign to final name "MISSING"
72+ MISSING = Sentinel("OTHER") # E: Cannot redefine an existing name as final
73+ [builtins fixtures/tuple.pyi]
74+
75+ [case testSentinelImplicitlyFinalWithLaterReference]
76+ from typing_extensions import Sentinel
77+
78+ _SENTINEL = Sentinel("_SENTINEL")
79+
80+ def func(x: int | _SENTINEL = _SENTINEL) -> int:
81+ if x is _SENTINEL:
82+ return 0
83+ return x
7384[builtins fixtures/tuple.pyi]
7485
7586[case testBuiltinsSentinelTypeExpression]
You can’t perform that action at this time.
0 commit comments