Skip to content

class.__annotations__ gives weird value when using built-in type keywords as attribute #122937

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

Open
Heerozh opened this issue Aug 12, 2024 · 5 comments
Assignees
Labels
docs Documentation in the Doc dir topic-typing

Comments

@Heerozh
Copy link

Heerozh commented Aug 12, 2024

Bug report

Bug description:

class some:
    int    : int = 1
    normal : int = 1
some.__annotations__

returns:
{'int': 1, 'normal': 1}

class some:
    int_   : int = 1
    normal : int = 1
some.__annotations__

returns:
{'int_': int, 'normal': int}

CPython versions tested on:

3.11, 3.12

Operating systems tested on:

Linux, Windows

Linked PRs

@Heerozh Heerozh added the type-bug An unexpected behavior, bug, or error label Aug 12, 2024
@sobolevn
Copy link
Member

sobolevn commented Aug 12, 2024

This is how names and scopes work in Python.

int: int = 1 means:

  • assign int as 1
  • use name int as annotation
  • which equals to 1, because you assigned it this way

Closing, since there were multiple issues about it and the anwser is always the same :)

@sobolevn sobolevn closed this as not planned Won't fix, can't repro, duplicate, stale Aug 12, 2024
@terryjreedy
Copy link
Member

Should anything be added to the docs (I don't know where) to explain this better?

@AlexWaygood
Copy link
Member

AlexWaygood commented Aug 12, 2024

For reference, the previous issues where this has been raised are:

It does seem to be coming up more and more regularly (for whatever reason), so if we can explain it better, that would be great... There's already documentation on this at https://docs.python.org/3/reference/simple_stmts.html#annassign, which is an appropriate place for this to be documented (since it's to do with core language semantics), but it's almost certainly not where beginners will think to look.

@sobolevn
Copy link
Member

Maybe in https://docs.python.org/3/howto/annotations.html and link from dataclasses docs to there?

I would be happy to add this note!

@sobolevn sobolevn reopened this Aug 12, 2024
@sobolevn sobolevn added docs Documentation in the Doc dir and removed type-bug An unexpected behavior, bug, or error labels Aug 12, 2024
@sobolevn sobolevn self-assigned this Aug 12, 2024
sobolevn added a commit to sobolevn/cpython that referenced this issue Aug 16, 2024
@Heerozh
Copy link
Author

Heerozh commented Aug 16, 2024

I have also submitted issues to pycharm. If implemented, it should help others understand this in the future.

https://youtrack.jetbrains.com/issue/PY-75106/type-annotation-security-check-for-shadowing-name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir topic-typing
Projects
None yet
Development

No branches or pull requests

4 participants