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 — Fix constructor calls for union-bounded TypeVars by 97littleleaf11 · Pull Request #21571 · python/mypy

Fix constructor calls for union-bounded TypeVars #21571

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
JukkaL merged 1 commit into python:master from 97littleleaf11:fix-21106
Jun 1, 2026

Uh oh!

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

Merged

Fix constructor calls for union-bounded TypeVars #21571

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
56ba5da
Fix constructor calls for union-bounded TypeVars
97littleleaf11 May 31, 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
Fix constructor calls for union-bounded TypeVars
  • Loading branch information

Uh oh!

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

@97littleleaf11
97littleleaf11 committed May 31, 2026
commit 56ba5dae7f7e5fb0e7787f1c83dbeebbeb55545c

There are no files selected for viewing

24 changes: 18 additions & 6 deletions 24 mypy/checkexpr.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1948,12 +1948,7 @@ def analyze_type_type_callee(self, item: ProperType, context: Context) -> Type:
# but better than AnyType...), but replace the return type
# with typevar.
callee = self.analyze_type_type_callee(get_proper_type(item.upper_bound), context)
callee = get_proper_type(callee)
if isinstance(callee, CallableType):
callee = callee.copy_modified(ret_type=item)
elif isinstance(callee, Overloaded):
callee = Overloaded([c.copy_modified(ret_type=item) for c in callee.items])
return callee
return self.replace_type_type_callee_ret_type(callee, item)
# We support Type of namedtuples but not of tuples in general
if isinstance(item, TupleType) and tuple_fallback(item).type.fullname != "builtins.tuple":
return self.analyze_type_type_callee(tuple_fallback(item), context)
Expand All @@ -1963,6 +1958,23 @@ def analyze_type_type_callee(self, item: ProperType, context: Context) -> Type:
self.msg.unsupported_type_type(item, context)
return AnyType(TypeOfAny.from_error)

def replace_type_type_callee_ret_type(self, callee: Type, ret_type: Type) -> Type:
callee = get_proper_type(callee)
if isinstance(callee, CallableType):
return callee.copy_modified(ret_type=ret_type)
if isinstance(callee, Overloaded):
return Overloaded([c.copy_modified(ret_type=ret_type) for c in callee.items])
if isinstance(callee, UnionType):
return UnionType(
[
self.replace_type_type_callee_ret_type(item, ret_type)
for item in callee.relevant_items()
],
line=callee.line,
column=callee.column,
)
return callee

def infer_arg_types_in_empty_context(self, args: list[Expression]) -> list[Type]:
"""Infer argument expression types in an empty context.

Expand Down
18 changes: 18 additions & 0 deletions 18 test-data/unit/check-classes.test
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3889,6 +3889,24 @@ def process(cls: Type[U]):
[builtins fixtures/classmethod.pyi]
[out]

[case testTypeUsingTypeCConstructorReturnFromTypeVarUnionBound]
from typing import Optional, Type, TypeVar, Union

class A:
def __init__(self, value: str = "") -> None: pass
class B:
def __init__(self, value: str = "") -> None: pass

T = TypeVar("T", bound=Union[A, B])

def make(ftype: Type[T], value: Optional[str]) -> T:
if value is None:
return ftype()
return ftype(value)

reveal_type(make(A, "a")) # N: Revealed type is "__main__.A"
reveal_type(make(B, None)) # N: Revealed type is "__main__.B"

[case testTypeUsingTypeCErrorUnsupportedType]
from typing import Type, Tuple
def foo(arg: Type[Tuple[int]]):
Expand Down
2 changes: 1 addition & 1 deletion 2 test-data/unit/check-python310.test
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3531,7 +3531,7 @@ def switch(choice: type[T_Choice]) -> None:
reveal_type(choice()) # N: Revealed type is "b.Two"
case _:
reveal_type(choice) # N: Revealed type is "type[T_Choice`-1]"
reveal_type(choice()) # N: Revealed type is "b.One | b.Two"
reveal_type(choice()) # N: Revealed type is "T_Choice`-1"

[file b.py]
class One: ...
Expand Down
14 changes: 14 additions & 0 deletions 14 test-data/unit/check-python312.test
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,20 @@ f(1, u)
f('x', None) # E: Value of type variable "T" of "f" cannot be "str" \
# E: Value of type variable "S" of "f" cannot be "None"

[case testPEP695UpperBoundTypeTypeConstructorReturnType]
class A:
def __init__(self, value: str = "") -> None: pass
class B:
def __init__(self, value: str = "") -> None: pass

def make[T: A | B](ftype: type[T], value: str | None) -> T:
if value is None:
return ftype()
return ftype(value)

reveal_type(make(A, "a")) # N: Revealed type is "__main__.A"
reveal_type(make(B, None)) # N: Revealed type is "__main__.B"

[case testPEP695InferVarianceOfTupleType]
class Cov[T](tuple[int, str]):
def f(self) -> T: pass
Expand Down
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.