MCPcopy Index your code
hub / github.com/python/mypy / Attribute

Class Attribute

mypy/plugins/attrs.py:110–238  ·  view source on GitHub ↗

The value of an attr.ib() call.

Source from the content-addressed store, hash-verified

108
109
110class Attribute:
111 """The value of an attr.ib() call."""
112
113 def __init__(
114 self,
115 name: str,
116 alias: str | None,
117 info: TypeInfo,
118 has_default: bool,
119 init: bool,
120 kw_only: bool,
121 converter: Converter | None,
122 context: Context,
123 init_type: Type | None,
124 ) -> None:
125 self.name = name
126 self.alias = alias
127 self.info = info
128 self.has_default = has_default
129 self.init = init
130 self.kw_only = kw_only
131 self.converter = converter
132 self.context = context
133 self.init_type = init_type
134
135 def argument(self, ctx: mypy.plugin.ClassDefContext) -> Argument:
136 """Return this attribute as an argument to __init__."""
137 assert self.init
138 init_type: Type | None = None
139 if self.converter:
140 if self.converter.init_type:
141 init_type = self.converter.init_type
142 if init_type and self.init_type and self.converter.ret_type:
143 # The converter return type should be the same type as the attribute type.
144 # Copy type vars from attr type to converter.
145 converter_vars = get_type_vars(self.converter.ret_type)
146 init_vars = get_type_vars(self.init_type)
147 if converter_vars and len(converter_vars) == len(init_vars):
148 variables = {
149 binder.id: arg for binder, arg in zip(converter_vars, init_vars)
150 }
151 init_type = expand_type(init_type, variables)
152 else:
153 ctx.api.fail("Cannot determine __init__ type from converter", self.context)
154 init_type = AnyType(TypeOfAny.from_error)
155 else: # There is no converter, the init type is the normal type.
156 init_type = self.init_type or self.info[self.name].type
157
158 unannotated = False
159 if init_type is None:
160 unannotated = True
161 # Convert type not set to Any.
162 init_type = AnyType(TypeOfAny.unannotated)
163 else:
164 proper_type = get_proper_type(init_type)
165 if isinstance(proper_type, AnyType):
166 if proper_type.type_of_any == TypeOfAny.unannotated:
167 unannotated = True

Callers 3

deserializeMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…