python / python/peps

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

Open
#3,062 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
reStructuredText
Stars
5k
Forks
1.8k
Avg merge
2d 4h
Merged PRs (30d)
25

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open PEP 526 and review the “Runtime Effects of Type Annotations” section, especially its module-level annotation example and displayed output. Confirm the name-mangling behavior against the example, then update the code or output so they agree and preserve a clear demonstration of class-level mangling. Done means the rendered PEP no longer presents contradictory results.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.