GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Respect explicit return type of __new__() by ilevkivskyi · Pull Request #21441 · python/mypy

Respect explicit return type of __new__() #21441

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.

Sign up for GitHub

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

Already on GitHub? Sign in to your account

Merged
ilevkivskyi merged 18 commits into python:master from ilevkivskyi:new-new
May 27, 2026
+627 −111
Merged

Respect explicit return type of __new__() #21441

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
b5ea90c
Preparatory refactor
ilevkivskyi May 5, 2026
a43ecc1
Add basic logic
ilevkivskyi May 6, 2026
c930a95
Add boilerplate
ilevkivskyi May 7, 2026
8c3e017
Do not use ret_type in various special cases
ilevkivskyi May 7, 2026
b01ffea
Store precise type
ilevkivskyi May 8, 2026
b76c746
Some more tweaks
ilevkivskyi May 8, 2026
73bb465
Update docstring
ilevkivskyi May 8, 2026
4434f73
Fix one more casual ret_type
ilevkivskyi May 8, 2026
b921130
Do not leak type variables in type[T]
ilevkivskyi May 8, 2026
e356348
Merge remote-tracking branch 'upstream/master' into new-new
ilevkivskyi May 11, 2026
7916bf1
Try alternative implementation of exceptions
ilevkivskyi May 11, 2026
daba172
Fix weird stuff
ilevkivskyi May 11, 2026
f8c159b
Merge remote-tracking branch 'upstream/master' into new-new
ilevkivskyi May 22, 2026
97791b4
Address CR
ilevkivskyi May 23, 2026
4519e3f
Skip TypeVars completely; inline the call
ilevkivskyi May 23, 2026
65869e1
Merge remote-tracking branch 'upstream/master' into new-new
ilevkivskyi May 26, 2026
97534e4
Add tests; fix inference
ilevkivskyi May 26, 2026
107ede7
Some more tests
ilevkivskyi May 27, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
Add boilerplate
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@ilevkivskyi
ilevkivskyi committed May 7, 2026
commit c930a9559d8d0d681f7893ce6b5837361cc5b7fc

There are no files selected for viewing

13 changes: 7 additions & 6 deletions 13 mypy/applytype.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,18 @@ def apply_generic_arguments(
assert isinstance(typ, TypeVarLikeType)
remaining_tvars.append(typ)

applied = callable.copy_modified(
instance_type = None
if callable.instance_type is not None:
instance_type = expand_type(callable.instance_type, id_to_type)
assert isinstance(instance_type, Instance)

return callable.copy_modified(
ret_type=expand_type(callable.ret_type, id_to_type),
variables=remaining_tvars,
type_guard=type_guard,
type_is=type_is,
instance_type=instance_type,
)
if callable.instance_type is not None:
repl_type = expand_type(callable.instance_type, id_to_type)
assert isinstance(repl_type, Instance)
applied.instance_type = repl_type
return applied


def apply_poly(tp: CallableType, poly_tvars: Sequence[TypeVarLikeType]) -> CallableType | None:
Expand Down
13 changes: 7 additions & 6 deletions 13 mypy/expandtype.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -493,16 +493,17 @@ def visit_callable_type(self, t: CallableType) -> CallableType:
arg_types = self.interpolate_args_for_unpack(t, var_arg.typ)
else:
arg_types = self.expand_types(t.arg_types)
instance_type = None
if t.instance_type is not None:
instance_type = t.instance_type.accept(self)
assert isinstance(instance_type, ProperType) and isinstance(instance_type, Instance)
expanded = t.copy_modified(
arg_types=arg_types,
ret_type=t.ret_type.accept(self),
type_guard=(t.type_guard.accept(self) if t.type_guard is not None else None),
type_is=(t.type_is.accept(self) if t.type_is is not None else None),
type_guard=t.type_guard.accept(self) if t.type_guard is not None else None,
type_is=t.type_is.accept(self) if t.type_is is not None else None,
instance_type=instance_type,
)
if t.instance_type is not None:
repl_type = t.instance_type.accept(self)
assert isinstance(repl_type, ProperType) and isinstance(repl_type, Instance)
expanded.instance_type = repl_type
if needs_normalization:
return expanded.with_normalized_var_args()
return expanded
Expand Down
2 changes: 2 additions & 0 deletions 2 mypy/fixup.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ def visit_callable_type(self, ct: CallableType) -> None:
ct.type_guard.accept(self)
if ct.type_is is not None:
ct.type_is.accept(self)
if ct.instance_type is not None:
ct.instance_type.accept(self)

def visit_overloaded(self, t: Overloaded) -> None:
for ct in t.items:
Expand Down
6 changes: 6 additions & 0 deletions 6 mypy/indirection.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ def visit_instance(self, t: types.Instance) -> None:
def visit_callable_type(self, t: types.CallableType) -> None:
self._visit_type_list(t.arg_types)
self._visit(t.ret_type)
if t.type_guard is not None:
self._visit(t.type_guard)
if t.type_is is not None:
self._visit(t.type_is)
if t.instance_type is not None:
self._visit(t.instance_type)
self._visit_type_tuple(t.variables)

def visit_overloaded(self, t: types.Overloaded) -> None:
Expand Down
12 changes: 12 additions & 0 deletions 12 mypy/join.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -773,10 +773,16 @@ def join_similar_callables(t: CallableType, s: CallableType) -> CallableType:
fallback = t.fallback
else:
fallback = s.fallback
instance_type = None
if t.instance_type is not None and s.instance_type is not None:
joined = join_types(t.instance_type, s.instance_type)
if isinstance(joined, Instance):
instance_type = joined
return t.copy_modified(
arg_types=arg_types,
arg_names=combine_arg_names(t, s),
ret_type=join_types(t.ret_type, s.ret_type),
instance_type=instance_type,
fallback=fallback,
name=None,
)
Expand Down Expand Up @@ -827,10 +833,16 @@ def combine_similar_callables(t: CallableType, s: CallableType) -> CallableType:
fallback = t.fallback
else:
fallback = s.fallback
instance_type = None
if t.instance_type is not None and s.instance_type is not None:
joined = join_types(t.instance_type, s.instance_type)
if isinstance(joined, Instance):
instance_type = joined
return t.copy_modified(
arg_types=arg_types,
arg_names=combine_arg_names(t, s),
ret_type=join_types(t.ret_type, s.ret_type),
instance_type=instance_type,
fallback=fallback,
name=None,
)
Expand Down
11 changes: 11 additions & 0 deletions 11 mypy/meet.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1182,9 +1182,20 @@ def meet_similar_callables(t: CallableType, s: CallableType) -> CallableType:
fallback = t.fallback
else:
fallback = s.fallback
if t.instance_type is None:
instance_type = s.instance_type
elif s.instance_type is None:
instance_type = t.instance_type
else:
meet = meet_types(t.instance_type, s.instance_type)
if isinstance(meet, Instance):
instance_type = meet
else:
instance_type = None
return t.copy_modified(
arg_types=arg_types,
ret_type=meet_types(t.ret_type, s.ret_type),
instance_type=instance_type,
fallback=fallback,
name=None,
)
Expand Down
3 changes: 3 additions & 0 deletions 3 mypy/server/astdiff.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,9 @@ def visit_callable_type(self, typ: CallableType) -> SnapshotItem:
typ.is_ellipsis_args,
snapshot_types(typ.variables),
typ.is_bound,
snapshot_optional_type(typ.type_guard),
snapshot_optional_type(typ.type_is),
snapshot_optional_type(typ.instance_type),
)

def normalize_callable_variables(self, typ: CallableType) -> CallableType:
Expand Down
6 changes: 6 additions & 0 deletions 6 mypy/server/astmerge.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,12 @@ def visit_callable_type(self, typ: CallableType) -> None:
# Fallback can be None for callable types that haven't been semantically analyzed.
if typ.fallback is not None:
typ.fallback.accept(self)
if typ.type_guard is not None:
typ.type_guard.accept(self)
if typ.type_is is not None:
typ.type_is.accept(self)
if typ.instance_type is not None:
typ.instance_type.accept(self)
for tv in typ.variables:
if isinstance(tv, TypeVarType):
tv.upper_bound.accept(self)
Expand Down
6 changes: 6 additions & 0 deletions 6 mypy/server/deps.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,12 @@ def visit_callable_type(self, typ: CallableType) -> list[str]:
for arg in typ.arg_types:
triggers.extend(self.get_type_triggers(arg))
triggers.extend(self.get_type_triggers(typ.ret_type))
if typ.type_guard is not None:
triggers.extend(self.get_type_triggers(typ.type_guard))
if typ.type_is is not None:
triggers.extend(self.get_type_triggers(typ.type_is))
if typ.instance_type is not None:
triggers.extend(self.get_type_triggers(typ.instance_type))
# fallback is a metaclass type for class objects, and is
# processed separately.
return triggers
Expand Down
13 changes: 7 additions & 6 deletions 13 mypy/subtypes.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -749,17 +749,18 @@ def visit_callable_type(self, left: CallableType) -> bool:
if is_protocol_implementation(left.fallback, right, skip=["__call__"]):
return True
if right.type.is_protocol and left.is_type_obj():
ret_type = get_proper_type(left.ret_type)
if isinstance(ret_type, TupleType):
ret_type = mypy.typeops.tuple_fallback(ret_type)
if isinstance(ret_type, Instance) and is_protocol_implementation(
ret_type, right, proper_subtype=self.proper_subtype, class_obj=True
instance_type = left.get_instance_type()
if isinstance(instance_type, Instance) and is_protocol_implementation(
instance_type, right, proper_subtype=self.proper_subtype, class_obj=True
):
return True
return self._is_subtype(left.fallback, right)
elif isinstance(right, TypeType):
# This is unsound, we don't check the __init__ signature.
return left.is_type_obj() and self._is_subtype(left.ret_type, right.item)
return left.is_type_obj() and self._is_subtype(
left.get_instance_type() if isinstance(right.item, Instance) else left.ret_type,
right.item,
)
else:
return False

Expand Down
18 changes: 15 additions & 3 deletions 18 mypy/type_visitor.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
ParamSpecType,
PartialType,
PlaceholderType,
ProperType,
RawExpressionType,
TupleType,
Type,
Expand Down Expand Up @@ -254,10 +255,17 @@ def visit_unpack_type(self, t: UnpackType, /) -> Type:
return UnpackType(t.type.accept(self))

def visit_callable_type(self, t: CallableType, /) -> Type:
instance_type = None
if t.instance_type is not None:
instance_type = t.instance_type.accept(self)
assert isinstance(instance_type, ProperType) and isinstance(instance_type, Instance)
return t.copy_modified(
arg_types=self.translate_type_list(t.arg_types),
ret_type=t.ret_type.accept(self),
variables=self.translate_variables(t.variables),
type_guard=t.type_guard.accept(self) if t.type_guard is not None else None,
type_is=t.type_is.accept(self) if t.type_is is not None else None,
instance_type=instance_type,
)

def visit_tuple_type(self, t: TupleType, /) -> Type:
Expand Down Expand Up @@ -415,7 +423,10 @@ def visit_instance(self, t: Instance, /) -> T:

def visit_callable_type(self, t: CallableType, /) -> T:
# FIX generics
return self.query_types(t.arg_types + [t.ret_type])
types = t.arg_types + [t.ret_type]
if t.instance_type is not None:
types.append(t.instance_type)
return self.query_types(types)

def visit_tuple_type(self, t: TupleType, /) -> T:
return self.query_types([t.partial_fallback] + t.items)
Expand Down Expand Up @@ -553,10 +564,11 @@ def visit_callable_type(self, t: CallableType, /) -> bool:
# Avoid allocating any objects here as an optimization.
args = self.query_types(t.arg_types)
ret = t.ret_type.accept(self)
inst = t.instance_type.accept(self) if t.instance_type is not None else False
if self.strategy == ANY_STRATEGY:
return args or ret
return args or ret or inst
else:
return args and ret
return args and ret and inst

def visit_tuple_type(self, t: TupleType, /) -> bool:
return self.query_types([t.partial_fallback] + t.items)
Expand Down
12 changes: 6 additions & 6 deletions 12 mypy/typeops.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -348,18 +348,18 @@ def class_callable(
else:
ret_type = default_ret_type

callable_type = init_type.copy_modified(
instance_type = default_ret_type
if isinstance(instance_type, TupleType):
instance_type = instance_type.partial_fallback
assert isinstance(instance_type, Instance)
return init_type.copy_modified(
ret_type=ret_type,
fallback=type_type,
name=info.name,
variables=variables,
special_sig=special_sig,
instance_type=instance_type,
)
instance_type = default_ret_type
if isinstance(instance_type, TupleType):
instance_type = instance_type.partial_fallback
callable_type.instance_type = instance_type
return callable_type


def map_type_from_supertype(typ: Type, sub_info: TypeInfo, super_info: TypeInfo) -> Type:
Expand Down
37 changes: 29 additions & 8 deletions 37 mypy/types.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2186,6 +2186,7 @@ def __init__(
from_concatenate: bool = False,
imprecise_arg_kinds: bool = False,
unpack_kwargs: bool = False,
instance_type: Instance | None = None,
) -> None:
super().__init__(line, column)
assert len(arg_types) == len(arg_kinds) == len(arg_names)
Expand Down Expand Up @@ -2220,7 +2221,7 @@ def __init__(
self.type_guard = type_guard
self.type_is = type_is
self.unpack_kwargs = unpack_kwargs
self.instance_type: Instance | None = None
self.instance_type = instance_type

def copy_modified(
self: CT,
Expand All @@ -2244,6 +2245,7 @@ def copy_modified(
from_concatenate: Bogus[bool] = _dummy,
imprecise_arg_kinds: Bogus[bool] = _dummy,
unpack_kwargs: Bogus[bool] = _dummy,
instance_type: Bogus[Instance | None] = _dummy,
) -> CT:
modified = CallableType(
arg_types=arg_types if arg_types is not _dummy else self.arg_types,
Expand Down Expand Up @@ -2274,8 +2276,8 @@ def copy_modified(
else self.imprecise_arg_kinds
),
unpack_kwargs=unpack_kwargs if unpack_kwargs is not _dummy else self.unpack_kwargs,
instance_type=instance_type if instance_type is not _dummy else self.instance_type,
)
modified.instance_type = self.instance_type
# Optimization: Only NewTypes are supported as subtypes since
# the class is effectively final, so we can use a cast safely.
return cast(CT, modified)
Expand Down Expand Up @@ -2313,10 +2315,10 @@ def is_type_obj(self) -> bool:
get_proper_type(self.ret_type), UninhabitedType
)

def type_object(self) -> mypy.nodes.TypeInfo:
def get_instance_type(self) -> ProperType:
assert self.is_type_obj()
if self.instance_type is not None:
return self.instance_type.type
return self.instance_type
ret = get_proper_type(self.ret_type)
if isinstance(ret, TypeVarType):
ret = get_proper_type(ret.upper_bound)
Expand All @@ -2326,8 +2328,12 @@ def type_object(self) -> mypy.nodes.TypeInfo:
ret = ret.fallback
if isinstance(ret, LiteralType):
ret = ret.fallback
assert isinstance(ret, Instance)
return ret.type
return ret

def type_object(self) -> mypy.nodes.TypeInfo:
instance_type = self.get_instance_type()
assert isinstance(instance_type, Instance)
return instance_type.type

def accept(self, visitor: TypeVisitor[T]) -> T:
return visitor.visit_callable_type(self)
Expand Down Expand Up @@ -2592,10 +2598,13 @@ def serialize(self) -> JsonDict:
"implicit": self.implicit,
"is_bound": self.is_bound,
"type_guard": self.type_guard.serialize() if self.type_guard is not None else None,
"type_is": (self.type_is.serialize() if self.type_is is not None else None),
"type_is": self.type_is.serialize() if self.type_is is not None else None,
"from_concatenate": self.from_concatenate,
"imprecise_arg_kinds": self.imprecise_arg_kinds,
"unpack_kwargs": self.unpack_kwargs,
"instance_type": (
self.instance_type.serialize() if self.instance_type is not None else None
),
Comment thread

@JukkaL JukkaL May 26, 2026

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Add an incremental test case?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
}

@classmethod
Expand All @@ -2616,15 +2625,21 @@ def deserialize(cls, data: JsonDict) -> CallableType:
type_guard=(
deserialize_type(data["type_guard"]) if data["type_guard"] is not None else None
),
type_is=(deserialize_type(data["type_is"]) if data["type_is"] is not None else None),
type_is=deserialize_type(data["type_is"]) if data["type_is"] is not None else None,
from_concatenate=data["from_concatenate"],
imprecise_arg_kinds=data["imprecise_arg_kinds"],
unpack_kwargs=data["unpack_kwargs"],
instance_type=(
Instance.deserialize(data["instance_type"])
if data["instance_type"] is not None
else None
),
)

def write(self, data: WriteBuffer) -> None:
write_tag(data, CALLABLE_TYPE)
self.fallback.write(data)
write_type_opt(data, self.instance_type)
write_flags(
data,
[
Expand All @@ -2650,6 +2665,11 @@ def write(self, data: WriteBuffer) -> None:
def read(cls, data: ReadBuffer) -> CallableType:
assert read_tag(data) == INSTANCE
fallback = Instance.read(data)
if (tag := read_tag(data)) == LITERAL_NONE:
instance_type = None
else:
assert tag == INSTANCE
instance_type = Instance.read(data)
(
is_ellipsis_args,
implicit,
Expand All @@ -2674,6 +2694,7 @@ def read(cls, data: ReadBuffer) -> CallableType:
from_concatenate=from_concatenate,
imprecise_arg_kinds=imprecise_arg_kinds,
unpack_kwargs=unpack_kwargs,
instance_type=instance_type,
)
assert read_tag(data) == END_TAG
return ret
Expand Down
3 changes: 3 additions & 0 deletions 3 mypy/typetraverser.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ def visit_callable_type(self, t: CallableType, /) -> None:
if t.type_is is not None:
t.type_is.accept(self)

if t.instance_type is not None:
t.instance_type.accept(self)

def visit_tuple_type(self, t: TupleType, /) -> None:
self.traverse_type_list(t.items)
t.partial_fallback.accept(self)
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.