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
Do not leak type variables in type[T]
  • Loading branch information

Uh oh!

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

@ilevkivskyi
ilevkivskyi committed May 8, 2026
commit b9211302d875fc5477facf3a969142aa39b4dfa8

There are no files selected for viewing

36 changes: 24 additions & 12 deletions 36 mypy/constraints.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,11 @@ def infer_constraints_for_callable(


def infer_constraints(
template: Type, actual: Type, direction: int, skip_neg_op: bool = False
template: Type,
actual: Type,
direction: int,
skip_neg_op: bool = False,
erase_types: bool = True,
) -> list[Constraint]:
"""Infer type constraints.

Expand Down Expand Up @@ -312,14 +316,14 @@ def infer_constraints(
# Return early on an empty branch.
return []
type_state.inferring.append((template, actual))
res = _infer_constraints(template, actual, direction, skip_neg_op)
res = _infer_constraints(template, actual, direction, skip_neg_op, erase_types)
type_state.inferring.pop()
return res
return _infer_constraints(template, actual, direction, skip_neg_op)
return _infer_constraints(template, actual, direction, skip_neg_op, erase_types)


def _infer_constraints(
template: Type, actual: Type, direction: int, skip_neg_op: bool
template: Type, actual: Type, direction: int, skip_neg_op: bool, erase_types: bool
) -> list[Constraint]:
orig_template = template
template = get_proper_type(template)
Expand Down Expand Up @@ -424,7 +428,7 @@ def _infer_constraints(
return []

# Remaining cases are handled by ConstraintBuilderVisitor.
return template.accept(ConstraintBuilderVisitor(actual, direction, skip_neg_op))
return template.accept(ConstraintBuilderVisitor(actual, direction, skip_neg_op, erase_types))


def _is_type_type(tp: ProperType) -> TypeGuard[TypeType | UnionType]:
Expand Down Expand Up @@ -659,14 +663,20 @@ class ConstraintBuilderVisitor(TypeVisitor[list[Constraint]]):
# TODO: The value may be None. Is that actually correct?
actual: ProperType

def __init__(self, actual: ProperType, direction: int, skip_neg_op: bool) -> None:
def __init__(
self, actual: ProperType, direction: int, skip_neg_op: bool, erase_types: bool
) -> None:
# Direction must be SUBTYPE_OF or SUPERTYPE_OF.
self.actual = actual
self.direction = direction
# Whether to skip polymorphic inference (involves inference in opposite direction)
# this is used to prevent infinite recursion when both template and actual are
# generic callables.
self.skip_neg_op = skip_neg_op
# Normally we should erase generic actual type when inferring against type[T]
# to avoid leaking type variables, see testGenericClassAsArgumentToType.
# The only exception is self-types in generic classes, where we set this to False.
self.erase_types = erase_types

# Trivial leaf types

Expand Down Expand Up @@ -1376,15 +1386,17 @@ def visit_overloaded(self, template: Overloaded) -> list[Constraint]:
def visit_type_type(self, template: TypeType) -> list[Constraint]:
if isinstance(self.actual, CallableType):
if self.actual.is_type_obj():
return infer_constraints(
template.item, self.actual.get_instance_type(), self.direction
)
instance_type = self.actual.get_instance_type()
if self.erase_types:
instance_type = erase_typevars(instance_type)
return infer_constraints(template.item, instance_type, self.direction)
return infer_constraints(template.item, self.actual.ret_type, self.direction)
elif isinstance(self.actual, Overloaded):
if self.actual.is_type_obj():
return infer_constraints(
template.item, self.actual.items[0].get_instance_type(), self.direction
)
instance_type = self.actual.items[0].get_instance_type()
if self.erase_types:
instance_type = erase_typevars(instance_type)
return infer_constraints(template.item, instance_type, self.direction)
return infer_constraints(template.item, self.actual.items[0].ret_type, self.direction)
elif isinstance(self.actual, TypeType):
return infer_constraints(template.item, self.actual.item, self.direction)
Expand Down
5 changes: 4 additions & 1 deletion 5 mypy/infer.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ def infer_type_arguments(
actual: Type,
is_supertype: bool = False,
skip_unsatisfied: bool = False,
erase_types: bool = True,
) -> list[Type | None]:
# Like infer_function_type_arguments, but only match a single type
# against a generic type.
constraints = infer_constraints(template, actual, SUPERTYPE_OF if is_supertype else SUBTYPE_OF)
constraints = infer_constraints(
template, actual, SUPERTYPE_OF if is_supertype else SUBTYPE_OF, erase_types=erase_types
)
return solve_constraints(type_vars, constraints, skip_unsatisfied=skip_unsatisfied)[0]
8 changes: 6 additions & 2 deletions 8 mypy/typeops.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ class B(A): pass

# Solve for these type arguments using the actual class or instance type.
typeargs = infer_type_arguments(
self_vars, self_param_type, original_type, is_supertype=True
self_vars, self_param_type, original_type, is_supertype=True, erase_types=False
)
if (
is_classmethod
Expand All @@ -504,7 +504,11 @@ class B(A): pass
):
# In case we call a classmethod through an instance x, fallback to type(x).
typeargs = infer_type_arguments(
self_vars, self_param_type, TypeType(original_type), is_supertype=True
self_vars,
self_param_type,
TypeType(original_type),
is_supertype=True,
erase_types=False,
)

# Update the method signature with the solutions found.
Expand Down
9 changes: 9 additions & 0 deletions 9 test-data/unit/check-generics.test
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3688,3 +3688,12 @@ reveal_type(ok3) # N: Revealed type is "tuple[()]"
bad1: list[()] = [] # E: "list" expects 1 type argument, but none given \
# E: Missing type arguments for generic type "list"
[builtins fixtures/tuple.pyi]

[case testGenericClassAsArgumentToType]
from typing import TypeVar, Generic

T = TypeVar("T")
def test(tp: type[T]) -> T: ...

class C(Generic[T]): ...
reveal_type(test(C)) # N: Revealed type is "__main__.C[Any]"
Loading
Oops, something went wrong.

Uh oh!

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

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.