MCPcopy
hub / github.com/python/mypy / NewTypeAnalyzer

Class NewTypeAnalyzer

mypy/semanal_newtype.py:46–275  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

44
45
46class NewTypeAnalyzer:
47 def __init__(
48 self, options: Options, api: SemanticAnalyzerInterface, msg: MessageBuilder
49 ) -> None:
50 self.options = options
51 self.api = api
52 self.msg = msg
53
54 def process_newtype_declaration(self, s: AssignmentStmt) -> bool:
55 """Check if s declares a NewType; if yes, store it in symbol table.
56
57 Return True if it's a NewType declaration. The current target may be
58 deferred as a side effect if the base type is not ready, even if
59 the return value is True.
60
61 The logic in this function mostly copies the logic for visit_class_def()
62 with a single (non-Generic) base.
63 """
64 var_name, call = self.analyze_newtype_declaration(s)
65 if var_name is None or call is None:
66 return False
67 name = var_name
68 # OK, now we know this is a NewType. But the base type may be not ready yet,
69 # add placeholder as we do for ClassDef.
70
71 if self.api.is_func_scope():
72 name += "@" + str(s.line)
73 fullname = self.api.qualified_name(name)
74
75 if not call.analyzed or isinstance(call.analyzed, NewTypeExpr) and not call.analyzed.info:
76 # Start from labeling this as a future class, as we do for normal ClassDefs.
77 placeholder = PlaceholderNode(fullname, s, s.line, becomes_typeinfo=True)
78 self.api.add_symbol(var_name, placeholder, s, can_defer=False)
79
80 old_type, should_defer = self.check_newtype_args(var_name, call, s)
81 old_type = get_proper_type(old_type)
82 if not isinstance(call.analyzed, NewTypeExpr):
83 call.analyzed = NewTypeExpr(var_name, old_type, line=call.line, column=call.column)
84 else:
85 call.analyzed.old_type = old_type
86 if old_type is None:
87 if should_defer:
88 # Base type is not ready.
89 self.api.defer()
90 return True
91
92 # Create the corresponding class definition if the aliased type is subtypeable
93 assert isinstance(call.analyzed, NewTypeExpr)
94 if isinstance(old_type, TupleType):
95 newtype_class_info = self.build_newtype_typeinfo(
96 name, old_type, old_type.partial_fallback, s.line, call.analyzed.info
97 )
98 newtype_class_info.update_tuple_type(old_type)
99 elif isinstance(old_type, Instance):
100 if old_type.type.is_protocol:
101 self.fail("NewType cannot be used with protocol classes", s)
102 newtype_class_info = self.build_newtype_typeinfo(
103 name, old_type, old_type, s.line, call.analyzed.info

Callers 1

file_contextMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…