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

Enable Final instance attributes for attrs #14232

Merged
merged 2 commits into from
Dec 6, 2022

Conversation

Tinche
Copy link
Contributor

@Tinche Tinche commented Dec 1, 2022

A quick patch to enable the following scenario:

@define
class C:
    a: Final[int]  # `a` is a final instance attribute

There are some edge cases I haven't covered here that would be complex to handle and not add much value IMO, so I think this will be useful like this.

For example:

@define
class C:
    a: Final[int] = field(init=False)

This is technically not sound, but there might be an __attrs_post_init__ on the class or one of its ancestors and that function might set it.

@github-actions

This comment has been minimized.

if attribute.name in ti.names:
sym_node = ti.names[attribute.name].node
if isinstance(sym_node, Var) and sym_node.is_final:
sym_node.final_set_in_init = True
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to potentially modify the symbol node of a base class. That is not safe to do, since we may have serialized an earlier version of the class, etc. So probably best to do this only for attributes defined in the current class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JukkaL Good catch, fix applied.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 6, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@JukkaL JukkaL merged commit 7e9aa74 into python:master Dec 6, 2022
@Tinche Tinche deleted the attrs/final-instance-attributes branch December 6, 2022 17:00
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

Successfully merging this pull request may close these issues.

None yet

2 participants