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

Method to_argument

mypy/plugins/dataclasses.py:127–159  ·  view source on GitHub ↗
(
        self, current_info: TypeInfo, *, of: Literal["__init__", "replace", "__post_init__"]
    )

Source from the content-addressed store, hash-verified

125 self._api = api
126
127 def to_argument(
128 self, current_info: TypeInfo, *, of: Literal["__init__", "replace", "__post_init__"]
129 ) -> Argument:
130 if of == "__init__":
131 arg_kind = ARG_POS
132 if self.kw_only and self.has_default:
133 arg_kind = ARG_NAMED_OPT
134 elif self.kw_only and not self.has_default:
135 arg_kind = ARG_NAMED
136 elif not self.kw_only and self.has_default:
137 arg_kind = ARG_OPT
138 elif of == "replace":
139 arg_kind = ARG_NAMED if self.is_init_var and not self.has_default else ARG_NAMED_OPT
140 elif of == "__post_init__":
141 # We always use `ARG_POS` without a default value, because it is practical.
142 # Consider this case:
143 #
144 # @dataclass
145 # class My:
146 # y: dataclasses.InitVar[str] = 'a'
147 # def __post_init__(self, y: str) -> None: ...
148 #
149 # We would be *required* to specify `y: str = ...` if default is added here.
150 # But, most people won't care about adding default values to `__post_init__`,
151 # because it is not designed to be called directly, and duplicating default values
152 # for the sake of type-checking is unpleasant.
153 arg_kind = ARG_POS
154 return Argument(
155 variable=self.to_var(current_info),
156 type_annotation=self.expand_type(current_info),
157 initializer=EllipsisExpr() if self.has_default else None, # Only used by stubgen
158 kind=arg_kind,
159 )
160
161 def expand_type(self, current_info: TypeInfo) -> Type | None:
162 if self.type is not None and self.info.self_type is not None:

Callers 4

transformMethod · 0.80
_add_dunder_replaceMethod · 0.80

Calls 4

to_varMethod · 0.95
expand_typeMethod · 0.95
ArgumentClass · 0.90
EllipsisExprClass · 0.90

Tested by

no test coverage detected