Skip to content

Try to fix problems caused by in-place modification of typevar defaults#19382

Closed
sterliakov wants to merge 3 commits into
python:masterfrom
sterliakov:bugfix/gh-19186-typevar-default-crash
Closed

Try to fix problems caused by in-place modification of typevar defaults#19382
sterliakov wants to merge 3 commits into
python:masterfrom
sterliakov:bugfix/gh-19186-typevar-default-crash

Conversation

@sterliakov
Copy link
Copy Markdown
Collaborator

@sterliakov sterliakov commented Jul 5, 2025

Fixes #19186. Fixes #19185. Fixes #19192. Fixes behavior in #14851 (comment) (note that assert_type should fail with bare class, it is still generic until actually used unsubscripted).

I do not know yet whether these changes are actually valid. Let's check the primer output and run all tests first.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Aug 8, 2025

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@sterliakov sterliakov marked this pull request as draft October 28, 2025 19:13
ilevkivskyi added a commit that referenced this pull request Nov 20, 2025
…20139)

Fixes #19185. Fixes #19186.

Replaces #19382 (not closing that yet, need to extract another part
related to `applytype` from there to another PR).

When a `TypeVar` defaults to another `TypeVar`, that default should
already be in scope. Fortunately we already analyze them in the correct
order, so the active scope has all the necessary information. This fixes
the namespace drift problem because the bound typevars are already
adjusted to use the correct namespace, and we do not need to touch their
global definitions.

I am not super happy about making TypeVarLikeScope impure here - any
suggestions? Would passing `self.msg` to every `bind_new` call look
saner?

---------

Co-authored-by: Ivan Levkivskyi <levkivskyi@gmail.com>
ilevkivskyi added a commit to ilevkivskyi/mypy that referenced this pull request May 21, 2026
)

Fixes python#19192
Fixes python#20027
Fixes few TODOs in tests (don't know if there are relevant issues)
Closes python#19382

Core idea is simple: don't do bizarre things :-) More precisely, instead
of various in-place modifications (which is a big no-no for types),
simply expand defaults iteratively as we build the environment for the
type. Couple additional things:
* Update defaults in `freshen_function_type_vars()` this is required for
consistency when updating `TypeVarId`s.
* Use `fix_instance()` in `checkexpr.py` for type applications. This
will apply _all_ type arguments in cases like `x = Foo[int](...)`. The
PEP and the spec say this is how it should be.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants