Skip to content

Comments

Enable --allow-redefinition-new in self-check#20877

Merged
ilevkivskyi merged 3 commits intopython:masterfrom
ilevkivskyi:enable-redefine-self
Feb 24, 2026
Merged

Enable --allow-redefinition-new in self-check#20877
ilevkivskyi merged 3 commits intopython:masterfrom
ilevkivskyi:enable-redefine-self

Conversation

@ilevkivskyi
Copy link
Member

Now that the performance issues are fixed, I think this is ready. I also undo previously added workarounds.

cc @JukkaL @hauntsaninja

@ilevkivskyi
Copy link
Member Author

Oh well, now there is a recursion error in mypyc, last time I tried this few days ago it worked, so must be something new.

@ilevkivskyi
Copy link
Member Author

The recursion error happens here:

        elif isinstance(typ, PartialType):
            assert typ.var.type is not None
            return self.type_to_rtype(typ.var.type)

so it looks like we somehow got a PartialType that points to itself, LOL.

@github-actions

This comment has been minimized.

@ilevkivskyi
Copy link
Member Author

Pushing a fix in a separate PR #20879

@github-actions

This comment has been minimized.

@ilevkivskyi
Copy link
Member Author

Minimal repro for the new issue:

[case testFooBar]
# flags: --allow-redefinition-new --local-partial-types
from typing import Optional

def foo() -> None:
    name: Optional[str] = None
    name = None
    for i in [1, 2, 3]:
        reveal_type(name)  # N: Revealed type is "None | builtins.str"
        if bool():
            name = "a"

This is caused by my binder PR #20862. I guess my understanding of the binder logic was wrong: current_value = None means no narrowing known in current frame, not in all enclosing frames. It is also surprising that this only breaks with --allow-redefinition-new while there is an explicit annotation. Probably bind_all also does something bad.

@ilevkivskyi
Copy link
Member Author

No, I actually checked .get() looks in all frames, so far this is a mystery.

@ilevkivskyi
Copy link
Member Author

ilevkivskyi commented Feb 24, 2026

Oh

                    changed = declaration_type is not None and not is_same_type(
                        type, declaration_type
                    )

should be

                   changed = changed or declaration_type is not None and not is_same_type(
                        type, declaration_type
                   )

pop didn't change because of i 🤦

@ilevkivskyi
Copy link
Member Author

Fix in a separate PR #20892

@github-actions
Copy link
Contributor

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

@ilevkivskyi ilevkivskyi merged commit 1abbafd into python:master Feb 24, 2026
24 checks passed
@ilevkivskyi ilevkivskyi deleted the enable-redefine-self branch February 24, 2026 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant