Skip to content
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.

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

Unexpected name mangling behavior with generics #119311

Closed
gaesa opened this issue May 21, 2024 · 1 comment · Fixed by #119464
Closed

Unexpected name mangling behavior with generics #119311

gaesa opened this issue May 21, 2024 · 1 comment · Fixed by #119464
Assignees
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes release-blocker topic-typing type-bug An unexpected behavior, bug, or error

Comments

@gaesa
Copy link

gaesa commented May 21, 2024

Bug report

Bug description:

Name mangling behaves inconsistently when used with generics. Here’s the code to reproduce the issue:

class __Foo(type):
    pass


class Bar[T](metaclass=__Foo):
    pass

This raises NameError: name '_Bar__Foo' is not defined. However, removing the [T] results in the error disappearing. This suggests that the name mangling behavior is inconsistent when generics are involved. This behavior seems to be a bug, as it would be expected for the name mangling to either always occur or never occur, regardless of whether generics are used.

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

@gaesa gaesa added the type-bug An unexpected behavior, bug, or error label May 21, 2024
@JelleZijlstra JelleZijlstra self-assigned this May 21, 2024
@JelleZijlstra
Copy link
Member

I agree this is a bug. We apply name mangling to all names that appear in the type parameter scope for the generic class (and also to all code after it, which is a separate bug: #119395). In the example, this means __Foo in the bases gets mangled to _Bar__Foo.

We could instead not do any mangling in the type parameter scope. That would fix this bug and simplify the code. However, it would break cases like this:

class X[__T]:
    def foo(self, x: __T): ...

If we didn't do mangling in the type parameter scope, the second use of __T would get mangled, but the first one wouldn't, so the second use of __T would fail to resolve.

Fortunately, the set of names that can appear in a type parameter scope is very limited. I think it should be possible to mangle only the type parameter names defined in the scope but nothing else. This would make both my example and the OP's example work, but it adds some complexity to the symtable.

@AlexWaygood AlexWaygood added topic-typing 3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels May 22, 2024
JelleZijlstra added a commit to JelleZijlstra/cpython that referenced this issue May 23, 2024
JelleZijlstra added a commit to JelleZijlstra/cpython that referenced this issue May 28, 2024
JelleZijlstra added a commit to JelleZijlstra/cpython that referenced this issue May 28, 2024
JelleZijlstra added a commit that referenced this issue May 28, 2024
Yhg1s pushed a commit that referenced this issue Jun 4, 2024
…9464) (#119644)

* [3.12] gh-119311: Fix name mangling with PEP 695 generic classes (#119464)

Fixes #119311. Fixes #119395.

(cherry picked from commit a9a74da)
JelleZijlstra pushed a commit that referenced this issue Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes release-blocker topic-typing type-bug An unexpected behavior, bug, or error
Projects
Development

Successfully merging a pull request may close this issue.

4 participants