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

PEP 526: Name mangling doesn't take place inside the body of a module #3062

Open
amirsoroush opened this issue Mar 17, 2023 · 1 comment
Open

Comments

@amirsoroush
Copy link

amirsoroush commented Mar 17, 2023

Hi,
In PEP 526, there is a section called "Runtime Effects of Type Annotations" which has the following code:

from typing import Dict
class Player:
    ...
players: Dict[str, Player]
__points: int

print(__annotations__)
# prints: {'players': typing.Dict[str, __main__.Player],
#          '_Player__points': <class 'int'>}

The code and the output don't match.
Name mangling only happens inside the body of the class not module. So the correct output would be:

{'players': typing.Dict[str, __main__.Player], '__points': <class 'int'>}

If we want to see the effect of name mangling we could have something like:

from typing import Dict

class Player:
    __points: int

players: Dict[str, Player]
print(__annotations__)
print(Player.__annotations__)

Or have the print(__annotations__) inside the class:

from typing import Dict

class Player:
    __points: int
    print(__annotations__)

players: Dict[str, Player]
print(__annotations__)

That would be a quick fix if you confirm.

@CAM-Gerlach
Copy link
Member

Not the expert here, but if I were to guess, perhaps something like the latter was intended and the indent was just not preserved correctly.

@JelleZijlstra ?

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

No branches or pull requests

3 participants
@CAM-Gerlach @amirsoroush and others