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 various crashes on recursive type variable defaults by ilevkivskyi · Pull Request #21491 · python/mypy

Fix various crashes on recursive type variable defaults #21491

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 13 commits into python:master from ilevkivskyi:fix-tv-default-crashes
May 26, 2026

Uh oh!

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

Merged

Fix various crashes on recursive type variable defaults #21491

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit Hold shift + click to select a range
7a07960
Add limited implementation
ilevkivskyi May 10, 2026
8efda98
Merge remote-tracking branch 'upstream/master' into fix-tv-default-cr…
ilevkivskyi May 14, 2026
cddf39f
Support non-recursive incomplete defaults
ilevkivskyi May 14, 2026
dbe1c63
Add more tests, fix new style classes
ilevkivskyi May 15, 2026
8597fc7
Some cleanup
ilevkivskyi May 15, 2026
43586b6
Fix self-compilation
ilevkivskyi May 15, 2026
e456cbe
Try better self-compilation fix
ilevkivskyi May 15, 2026
0f47216
Fix base classes
ilevkivskyi May 15, 2026
c0e7eda
Add custom note in case Any was forced
ilevkivskyi May 15, 2026
3d3a217
Merge remote-tracking branch 'upstream/master' into fix-tv-default-cr…
ilevkivskyi May 20, 2026
459adb1
Merge remote-tracking branch 'upstream/master' into fix-tv-default-cr…
ilevkivskyi May 21, 2026
c6c8493
Merge remote-tracking branch 'upstream/master' into fix-tv-default-cr…
ilevkivskyi May 23, 2026
afc0cdc
Fix typo
ilevkivskyi May 26, 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 custom note in case Any was forced
  • Loading branch information

Uh oh!

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

@ilevkivskyi
ilevkivskyi committed May 15, 2026
commit c0e7eda9e676df89f993de7476c0922a174ee92a

There are no files selected for viewing

1 change: 1 addition & 0 deletions 1 mypy/checkexpr.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -4919,6 +4919,7 @@ def visit_type_application(self, tapp: TypeApplication) -> Type:
tapp.expr.node,
tapp.types,
self.chk.fail,
self.chk.note,
tapp.expr.node.no_args,
tapp,
self.chk.options,
Expand Down
1 change: 1 addition & 0 deletions 1 mypy/message_registry.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
IMPLICIT_GENERIC_ANY_BUILTIN: Final = (
'Implicit generic "Any". Use "{}" and specify generic parameters'
)
NO_CYCLIC_DEFAULT: Final = "Cyclic type variable defaults are not supported"
INVALID_UNPACK: Final = "{} cannot be unpacked (must be tuple or TypeVarTuple)"
INVALID_UNPACK_POSITION: Final = "Unpack is only valid in a variadic position"
INVALID_PARAM_SPEC_LOCATION: Final = "Invalid location for ParamSpec {}"
Expand Down
63 changes: 33 additions & 30 deletions 63 mypy/typeanal.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ def visit_unbound_type_nonoptional(self, t: UnboundType, defining_literal: bool)
node,
an_args,
self.fail,
self.note,
node.no_args,
t,
self.options,
Expand Down Expand Up @@ -908,6 +909,7 @@ def analyze_type_with_type_info(
# TODO: should we allow NamedTuples generic in ParamSpec?
self.anal_array(args, allow_unpack=True),
self.fail,
self.note,
False,
ctx,
self.options,
Expand All @@ -932,6 +934,7 @@ def analyze_type_with_type_info(
# TODO: should we allow TypedDicts generic in ParamSpec?
self.anal_array(args, allow_unpack=True),
self.fail,
self.note,
False,
ctx,
self.options,
Expand Down Expand Up @@ -2051,6 +2054,7 @@ def get_omitted_any(
options: Options,
fullname: str | None = None,
unexpanded_type: Type | None = None,
used_default: bool = False,
) -> AnyType:
if disallow_any:
typ = unexpanded_type or orig_type
Expand All @@ -2061,6 +2065,8 @@ def get_omitted_any(
typ,
code=codes.TYPE_ARG,
)
if used_default:
note(message_registry.NO_CYCLIC_DEFAULT, typ, code=codes.TYPE_ARG)

any_type = AnyType(TypeOfAny.from_error, line=typ.line, column=typ.column)
else:
Expand Down Expand Up @@ -2130,7 +2136,14 @@ def fix_instance(
if any_type is None:
fullname = None if use_generic_error else t.type.fullname
any_type = get_omitted_any(
disallow_any, fail, note, t, options, fullname, unexpanded_type
disallow_any,
fail,
note,
t,
options,
fullname,
unexpanded_type,
used_default,
)
arg = any_type
else:
Expand All @@ -2151,6 +2164,7 @@ def instantiate_type_alias(
node: TypeAlias,
args: list[Type],
fail: MsgCallback,
note: MsgCallback,
no_args: bool,
ctx: Context,
options: Options,
Expand Down Expand Up @@ -2180,15 +2194,7 @@ def instantiate_type_alias(
if any(unknown_unpack(a) for a in args):
# This type is not ready to be validated, because of unknown total count.
# Note that we keep the kind of Any for consistency.
return set_any_tvars(
node,
[],
ctx.line,
ctx.column,
options,
special_form=True,
analyzing_tvar_def=analyzing_tvar_def,
)
return set_any_tvars(node, [], ctx.line, ctx.column, options, special_form=True)

if (
no_args
Expand All @@ -2215,6 +2221,7 @@ def instantiate_type_alias(
options,
disallow_any=disallow_any,
fail=fail,
note=note,
unexpanded_type=unexpanded_type,
analyzing_tvar_def=analyzing_tvar_def,
)
Expand All @@ -2241,15 +2248,7 @@ def instantiate_type_alias(
if any(isinstance(a, UnpackType) for a in args):
# A variadic unpack in fixed size alias (fixed unpacks must be flattened by the caller)
fail(message_registry.INVALID_UNPACK_POSITION, ctx, code=codes.VALID_TYPE)
return set_any_tvars(
node,
[],
ctx.line,
ctx.column,
options,
from_error=True,
analyzing_tvar_def=analyzing_tvar_def,
)
return set_any_tvars(node, [], ctx.line, ctx.column, options, from_error=True)
min_tv_count = sum(not tv.has_default() for tv in node.alias_tvars)
fill_typevars = act_len != max_tv_count
correct = min_tv_count <= act_len <= max_tv_count
Expand Down Expand Up @@ -2296,7 +2295,10 @@ def instantiate_type_alias(
ctx.line,
ctx.column,
options,
from_error=True,
disallow_any=disallow_any,
fail=fail,
note=note,
from_error=not correct,
analyzing_tvar_def=analyzing_tvar_def,
)
elif node.tvar_tuple_index is not None:
Expand All @@ -2312,15 +2314,7 @@ def instantiate_type_alias(
act_suffix = len(args) - unpack - 1
if act_prefix < exp_prefix or act_suffix < exp_suffix:
fail("TypeVarTuple cannot be split", ctx, code=codes.TYPE_ARG)
return set_any_tvars(
node,
[],
ctx.line,
ctx.column,
options,
from_error=True,
analyzing_tvar_def=analyzing_tvar_def,
)
return set_any_tvars(node, [], ctx.line, ctx.column, options, from_error=True)
# TODO: we need to check args validity w.r.t alias.alias_tvars.
# Otherwise invalid instantiations will be allowed in runtime context.
# Note: in type context, these will be still caught by semanal_typeargs.
Expand Down Expand Up @@ -2348,6 +2342,7 @@ def set_any_tvars(
disallow_any: bool = False,
special_form: bool = False,
fail: MsgCallback | None = None,
note: MsgCallback | None = None,
unexpanded_type: Type | None = None,
analyzing_tvar_def: bool = False,
) -> tuple[TypeAliasType, bool]:
Expand All @@ -2374,6 +2369,7 @@ def set_any_tvars(
used_default = True
if is_typevar_default_recursive(tv.fullname, node):
arg = any_type
used_any_type = True
else:
arg = any_type
used_any_type = True
Expand All @@ -2390,7 +2386,7 @@ def set_any_tvars(
assert isinstance(fixed, TypeAliasType)
t.args = fixed.args

if used_any_type and disallow_any and node.alias_tvars:
if used_any_type and disallow_any and node.alias_tvars and not from_error:
assert fail is not None
if unexpanded_type:
type_str = (
Expand All @@ -2406,6 +2402,13 @@ def set_any_tvars(
Context(newline, newcolumn),
code=codes.TYPE_ARG,
)
if used_default:
assert note is not None
note(
message_registry.NO_CYCLIC_DEFAULT,
Context(newline, newcolumn),
code=codes.TYPE_ARG,
)
return t, used_default


Expand Down
31 changes: 31 additions & 0 deletions 31 test-data/unit/check-flags.test
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2717,3 +2717,34 @@ if (
or z is None
):
pass

[case testRecursiveTypeVarDefaultMutualDisallow]
# flags: --disallow-any-generic
from typing import TypeVar, Generic

class C(Generic["T"]):
pass

class D(Generic["S"]):
pass

T = TypeVar("T", default=D) # E: Missing type arguments for generic type "D" \
# N: Cyclic type variable defaults are not supported
S = TypeVar("S", default=C) # E: Missing type arguments for generic type "C" \
# N: Cyclic type variable defaults are not supported

c: C
d: D
reveal_type(c) # N: Revealed type is "__main__.C[__main__.D[Any]]"
reveal_type(d) # N: Revealed type is "__main__.D[__main__.C[Any]]"

[case testRecursiveTypeVarDefaultOnlyAliasDisallow]
# flags: --disallow-any-generic
from typing import TypeVar

T = TypeVar("T", default="A") # E: Missing type arguments for generic type "A" \
# N: Cyclic type variable defaults are not supported
A = list[T]
a: A
reveal_type(a) # N: Revealed type is "builtins.list[builtins.list[Any]]"
[builtins fixtures/tuple.pyi]
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.