Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Mypy micro-optimizations (batch 1/3)#19768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
hauntsaninja merged 4 commits intomasterfrommypy-opts-1
Aug 31, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletionmypy/applytype.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -243,7 +243,7 @@ def visit_callable_type(self, t: CallableType) -> Type:
self.bound_tvars -= set(found_vars)

assert isinstance(result, ProperType) and isinstance(result, CallableType)
result.variables =list(result.variables) + found_vars
result.variables = result.variables +tuple(found_vars)
return result

def visit_type_var(self, t: TypeVarType) -> Type:
Expand Down
8 changes: 5 additions & 3 deletionsmypy/binder.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -250,10 +250,12 @@ def update_from_options(self, frames: list[Frame]) -> bool:
options are the same.
"""
all_reachable = all(not f.unreachable for f in frames)
frames = [f for f in frames if not f.unreachable]
if not all_reachable:
frames = [f for f in frames if not f.unreachable]
changed = False
keys = {key for f in frames for key in f.types}

keys = [key for f in frames for key in f.types]
if len(keys) > 1:
keys = list(set(keys))
for key in keys:
current_value = self._get(key)
resulting_values = [f.types.get(key, current_value) for f in frames]
Expand Down
2 changes: 1 addition & 1 deletionmypy/plugins/common.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -360,7 +360,7 @@ def _add_method_by_spec(

signature = CallableType(arg_types, arg_kinds, arg_names, return_type, function_type)
if tvar_defs:
signature.variables = tvar_defs
signature.variables =tuple(tvar_defs)

func = FuncDef(name, args, Block([PassStmt()]))
func.info = info
Expand Down
2 changes: 1 addition & 1 deletionmypy/semanal.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -995,7 +995,7 @@ def analyze_func_def(self, defn: FuncDef) -> None:
if has_self_type and self.type is not None:
info = self.type
if info.self_type is not None:
result.variables =[info.self_type] +list(result.variables)
result.variables =(info.self_type,) + result.variables
defn.type = result
self.add_type_alias_deps(analyzer.aliases_used)
self.check_function_signature(defn)
Expand Down
2 changes: 1 addition & 1 deletionmypy/semanal_namedtuple.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -606,7 +606,7 @@ def add_method(
arg_kinds = [arg.kind for arg in args]
assert None not in types
signature = CallableType(cast(list[Type], types), arg_kinds, items, ret, function_type)
signature.variables =[self_type]
signature.variables =(self_type,)
func = FuncDef(funcname, args, Block([]))
func.info = info
func.is_class = is_classmethod
Expand Down
2 changes: 1 addition & 1 deletionmypy/server/aststrip.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -165,7 +165,7 @@ def visit_func_def(self, node: FuncDef) -> None:
# in order to get the state exactly as it was before semantic analysis.
# See also #4814.
assert isinstance(node.type, CallableType)
node.type.variables =[]
node.type.variables =()
with self.enter_method(node.info) if node.info else nullcontext():
super().visit_func_def(node)

Expand Down
15 changes: 9 additions & 6 deletionsmypy/type_visitor.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -228,7 +228,7 @@ def visit_instance(self, t: Instance, /) -> Type:
last_known_value = raw_last_known_value
return Instance(
typ=t.type,
args=self.translate_types(t.args),
args=self.translate_type_tuple(t.args),
line=t.line,
column=t.column,
last_known_value=last_known_value,
Expand All@@ -242,7 +242,7 @@ def visit_param_spec(self, t: ParamSpecType, /) -> Type:
return t

def visit_parameters(self, t: Parameters, /) -> Type:
return t.copy_modified(arg_types=self.translate_types(t.arg_types))
return t.copy_modified(arg_types=self.translate_type_list(t.arg_types))

def visit_type_var_tuple(self, t: TypeVarTupleType, /) -> Type:
return t
Expand All@@ -255,14 +255,14 @@ def visit_unpack_type(self, t: UnpackType, /) -> Type:

def visit_callable_type(self, t: CallableType, /) -> Type:
return t.copy_modified(
arg_types=self.translate_types(t.arg_types),
arg_types=self.translate_type_list(t.arg_types),
ret_type=t.ret_type.accept(self),
variables=self.translate_variables(t.variables),
)

def visit_tuple_type(self, t: TupleType, /) -> Type:
return TupleType(
self.translate_types(t.items),
self.translate_type_list(t.items),
# TODO: This appears to be unsafe.
cast(Any, t.partial_fallback.accept(self)),
t.line,
Expand DownExpand Up@@ -299,7 +299,7 @@ def visit_union_type(self, t: UnionType, /) -> Type:
return cached

result = UnionType(
self.translate_types(t.items),
self.translate_type_list(t.items),
t.line,
t.column,
uses_pep604_syntax=t.uses_pep604_syntax,
Expand All@@ -308,9 +308,12 @@ def visit_union_type(self, t: UnionType, /) -> Type:
self.set_cached(t, result)
return result

deftranslate_types(self, types:Iterable[Type]) -> list[Type]:
deftranslate_type_list(self, types:list[Type]) -> list[Type]:
return [t.accept(self) for t in types]

def translate_type_tuple(self, types: tuple[Type, ...]) -> tuple[Type, ...]:
return tuple(t.accept(self) for t in types)

def translate_variables(
self, variables: Sequence[TypeVarLikeType]
) -> Sequence[TypeVarLikeType]:
Expand Down
6 changes: 3 additions & 3 deletionsmypy/typeanal.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1820,7 +1820,7 @@ def infer_type_variables(

def bind_function_type_variables(
self, fun_type: CallableType, defn: Context
) -> tuple[Sequence[TypeVarLikeType], bool]:
) -> tuple[tuple[TypeVarLikeType, ...], bool]:
"""Find the type variables of the function type and bind them in our tvar_scope"""
has_self_type = False
if fun_type.variables:
Expand All@@ -1835,7 +1835,7 @@ def bind_function_type_variables(
assert isinstance(var_expr, TypeVarLikeExpr)
binding = self.tvar_scope.bind_new(var.name, var_expr)
defs.append(binding)
return defs, has_self_type
returntuple(defs), has_self_type
typevars, has_self_type = self.infer_type_variables(fun_type)
# Do not define a new type variable if already defined in scope.
typevars = [
Expand All@@ -1849,7 +1849,7 @@ def bind_function_type_variables(
binding = self.tvar_scope.bind_new(name, tvar)
defs.append(binding)

return defs, has_self_type
returntuple(defs), has_self_type

def is_defined_type_var(self, tvar: str, context: Context) -> bool:
tvar_node = self.lookup_qualified(tvar, context)
Expand Down
12 changes: 7 additions & 5 deletionsmypy/types.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2104,14 +2104,12 @@ def __init__(
) -> None:
super().__init__(line, column)
assert len(arg_types) == len(arg_kinds) == len(arg_names)
for t, k in zip(arg_types, arg_kinds):
self.arg_types = list(arg_types)
for t in self.arg_types:
if isinstance(t, ParamSpecType):
assert not t.prefix.arg_types
# TODO: should we assert that only ARG_STAR contain ParamSpecType?
# See testParamSpecJoin, that relies on passing e.g `P.args` as plain argument.
if variables is None:
variables = []
self.arg_types = list(arg_types)
self.arg_kinds = arg_kinds
self.arg_names = list(arg_names)
self.min_args = arg_kinds.count(ARG_POS)
Expand All@@ -2123,7 +2121,11 @@ def __init__(
# * If it is a non-decorated function, FuncDef is the definition
# * If it is a decorated function, enclosing Decorator is the definition
self.definition = definition
self.variables = variables
self.variables: tuple[TypeVarLikeType, ...]
if variables is None:
self.variables = ()
else:
self.variables = tuple(variables)
Comment on lines -2126 to +2128
Copy link
Collaborator

@cdce8pcdce8pSep 1, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Note, this does crash thepydantic.v1 extension.

File"/.../python3.13/site-packages/pydantic/v1/mypy.py",line868,inadd_methodsignature.variables= [tvar_def]^^^^^^^^^^^^^^^^^^^TypeError:tupleobjectexpected;gotlist

https://github.com/pydantic/pydantic/blob/v2.11.7/pydantic/v1/mypy.py#L866-L868

It can fairly easily be fixed and I'll open a PR for it soon. However, that might not be the only plugin which depends on it being "just" a sequence.

hauntsaninja reacted with thumbs up emoji
Copy link
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

If we have evidence that this impacts multiple plugins, we can consider reverting this change.

self.is_ellipsis_args = is_ellipsis_args
self.implicit = implicit
self.special_sig = special_sig
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp