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

Argument names in __annotations__ are not mangled for functions defined inside class scope #64824

Closed
jonasw mannequin opened this issue Feb 14, 2014 · 12 comments
Closed
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@jonasw
Copy link
Mannequin

jonasw mannequin commented Feb 14, 2014

BPO 20625
Nosy @ncoghlan, @larryhastings, @benjaminp, @merwok, @1st1
Files
  • issue_20625_01.patch
  • issue_20625_02.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/1st1'
    closed_at = <Date 2014-02-18.17:56:03.804>
    created_at = <Date 2014-02-14.11:57:28.716>
    labels = ['interpreter-core', 'type-bug']
    title = 'Argument names in __annotations__ are not mangled for functions defined inside class scope'
    updated_at = <Date 2014-03-17.06:31:02.344>
    user = 'https://bugs.python.org/jonasw'

    bugs.python.org fields:

    activity = <Date 2014-03-17.06:31:02.344>
    actor = 'python-dev'
    assignee = 'yselivanov'
    closed = True
    closed_date = <Date 2014-02-18.17:56:03.804>
    closer = 'yselivanov'
    components = ['Interpreter Core']
    creation = <Date 2014-02-14.11:57:28.716>
    creator = 'jonasw'
    dependencies = []
    files = ['34083', '34085']
    hgrepos = []
    issue_num = 20625
    keywords = ['patch']
    message_count = 12.0
    messages = ['211215', '211231', '211234', '211237', '211242', '211243', '211244', '211248', '211531', '211556', '211671', '213822']
    nosy_count = 9.0
    nosy_names = ['nnorwitz', 'ncoghlan', 'larry', 'benjamin.peterson', 'eric.araujo', 'Arfrever', 'python-dev', 'yselivanov', 'jonasw']
    pr_nums = []
    priority = 'high'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue20625'
    versions = ['Python 3.3', 'Python 3.4']

    @jonasw
    Copy link
    Mannequin Author

    jonasw mannequin commented Feb 14, 2014

    Assume I have this code:

    class Spam:
        def eggs(__some_kwarg:int=None):
            print(__some_kwarg)

    I can call Spam.bar with keyword arguments as expected from mangling:

    >>> Spam.eggs(10)
    10
    >>> Spam.eggs(_Spam__some_kwarg=10)
    10

    However, in the __annotations__ field, the argument name is not mangled:

    >>> Spam.eggs.__annotations__
    {'__some_kwarg': <class 'int'>}

    This is an inconsistency which makes it difficult to work with function annotations in this case.

    @jonasw jonasw mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Feb 14, 2014
    @1st1
    Copy link
    Member

    1st1 commented Feb 14, 2014

    It looks like it's a bug.

    Spam.eggs.__code__.co_varnames will have '_Spam__some_kwarg' as a name of the first parameter. So __annotations__ should have '_Spam__some_kwarg' instead of '__some_kwarg'.

    @1st1
    Copy link
    Member

    1st1 commented Feb 14, 2014

    Furthermore:

    class Foo:
      def bar(self, *, __kw:'anno'='default'):
        pass
    >>> Foo.bar.__annotations__
    {'__kw': 'anno'}
    >>> Foo.bar.__kwdefaults__
    {'_Foo__kw': 'default'}

    @1st1
    Copy link
    Member

    1st1 commented Feb 14, 2014

    Patch is attached. Please review.

    @larryhastings
    Copy link
    Contributor

    Why would we mangle the names of arguments in the first place? What possible use case is there?

    @1st1
    Copy link
    Member

    1st1 commented Feb 14, 2014

    Why would we mangle the names of arguments in the first place? What possible use case is there?

    The use case is multiple inheritance. You can define your methods with keyword and keyword-only params starting with '__', put in them some cached default for speeding up lookup time, and not being worried that someone can override the arg. The second reason is, I believe, is desire to be consistent with mangling methods and attributes.

    Again, using mangling for function arguments is, probably, not the best practice, and that, combined with the fact, that people are just starting to play with annotations, explains why this bug was so long unnoticed.

    But the semantics of arg names mangling is already there, implemented everywhere but __annotations__.

    The attached patch is fairly simple, and really is just a bug fix. It would be great if we can get this in 3.4 (and even in 3.3 and 3.2).

    @benjaminp
    Copy link
    Contributor

    (see review on rietveld)

    @benjaminp
    Copy link
    Contributor

    lgtm

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Feb 18, 2014

    New changeset a63327162063 by Yury Selivanov in branch 'default':
    Mangle __parameters in __annotations__ dict properly. Issue bpo-20625.
    http://hg.python.org/cpython/rev/a63327162063

    @1st1 1st1 closed this as completed Feb 18, 2014
    @1st1 1st1 self-assigned this Feb 18, 2014
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Feb 18, 2014

    New changeset 5202aca8a673 by Victor Stinner in branch 'default':
    Issue bpo-20625: Fix compilation issue
    http://hg.python.org/cpython/rev/5202aca8a673

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Feb 19, 2014

    New changeset e301a515f8f4 by Benjamin Peterson in branch 'default':
    update magic number for bpo-20625
    http://hg.python.org/cpython/rev/e301a515f8f4

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Mar 17, 2014

    New changeset 4d7c3cbd8515 by Yury Selivanov in branch '3.4':
    Mangle __parameters in __annotations__ dict properly. Issue bpo-20625.
    http://hg.python.org/cpython/rev/4d7c3cbd8515

    New changeset 3bced76d2706 by Victor Stinner in branch '3.4':
    Issue bpo-20625: Fix compilation issue
    http://hg.python.org/cpython/rev/3bced76d2706

    New changeset e5cde3fd7c74 by Benjamin Peterson in branch '3.4':
    update magic number for bpo-20625
    http://hg.python.org/cpython/rev/e5cde3fd7c74

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants