Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add conformance test for@final combined with@property#1916

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
samwgoldman wants to merge3 commits intopython:main
base:main
Choose a base branch
Loading
fromsamwgoldman:final-property

Conversation

samwgoldman
Copy link

The language of the typing spec indicates that the@final decorator may be used with properties:

The method decorator version may be used with all of instance methods, class methods, static methods, and properties.

I noticed this case while working on Pyre. I observed that Mypy and Pyright also differ in behavior, so it seemed like a good conformance test to add.

See discussion here:
https://discuss.python.org/t/proposal-allow-chaining-final-decorator-when-previous-decorators-return-a-non-function/78918

I also added an example to the spec itself. I think it's a practical example, but also happy to remove it as well, since the existing content makes it pretty clear that the example should be allowed.

The language of the typing spec indicates that the@Final decoratormay be used with properties:> The method decorator version may be used with all of instance methods, class methods, static methods, and properties.I noticed this case while working on Pyre. I observed that Mypy andPyright also differ in behavior, so it seemed like a good conformancetest to add.See discussion here:https://discuss.python.org/t/proposal-allow-chaining-final-decorator-when-previous-decorators-return-a-non-function/78918
I thought it would be nice to add an example showing the use of@Finaland@Property together, but I also think the existing content isreasonably clear that this example is supposed to work.
@ghost
Copy link

ghost commentedJan 31, 2025
edited by ghost
Loading

All commit authors signed the Contributor License Agreement.
CLA signed

Copy link
Collaborator

@hauntsaninjahauntsaninja left a comment

Choose a reason for hiding this comment

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

Looks good, although I think conformance in pyright/qualifiers_final_decorator.toml should be updated to match the new conformance_automated value

samwgoldman reacted with thumbs up emoji
@@ -1,4 +1,4 @@
conformant = "Pass"
conformant = "Fail"
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should say "Partial", not "Fail".

@erictraut
Copy link
Collaborator

Thanks for reporting this and adding the conformance test. FYI, I've filed the following bug in the pyright issue tracker:microsoft/pyright#9795.

@samwgoldman
Copy link
Author

Thanks for the guidance!

@hauntsaninja
Copy link
Collaborator

I guess always a risk changing a pyright conformant value, because odds are 50/50 that Eric will have fixed it before the PR is merged :-)

erictraut and AlexWaygood reacted with laugh emojicarljm, JelleZijlstra, and AlexWaygood reacted with rocket emoji

@@ -66,6 +66,22 @@ implementation (or on the first overload, for stubs)::
def method(self, x=None):
...

The ``@final`` decorator can be combined with previous decorators, like

Choose a reason for hiding this comment

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

Somebody on Discuss brought up that perhaps you should put@property above@final. Do we need to specify that explicitly?

Forclassmethod +final, currently bothmypy andpyright allow putting the decorators in any order.

My preference would be to make the spec say explicitly that either order is allowed and add conformance tests for it.

Copy link
Collaborator

@erictrauterictrautJan 31, 2025
edited
Loading

Choose a reason for hiding this comment

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

What is the correct behavior if a property getter is marked final but a setter or deleter is not (or vice versa)? I ask because the different ordering would seem to imply different behaviors. Does thefinal apply to theproperty object or the method that is wrapped by the property?

Copy link
Author

Choose a reason for hiding this comment

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

I think it's a good point that the spec text here it too specific ("previous" implies an order, should read "other").

On the other hand, I think it gets a bit too precise to enumerate orders. In Pyre (and I suspect Pyright too) the non-conformance arises from attaching the decorator metadata directly to a function type, so a chained decorator that returns a non-function runs into problems.

In terms of typing, the@property and@final signatures compose in either order, so I think it's clear that the definition is legal in either order.

Copy link
Author

@samwgoldmansamwgoldmanJan 31, 2025
edited
Loading

Choose a reason for hiding this comment

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

What is the correct behavior if a property getter is marked final but a setter or deleter is not (or vice versa)? I ask because the different ordering would seem to imply different behaviors. Does thefinal apply to theproperty object or the method that is wrapped by the property?

This is a great question. I don't feel strongly, but one option:

  1. Thefinal-ness applies to the slot on the class, not the type itself (property descriptor or otherwise)
  2. If the final property is a descriptor, then the setter should not be called on that type (either throughp: Final = Descriptor() or@final-decorated descriptor)
  3. [Edited to add] Specifically for the@property descriptor, defining a setter/deleter is an error when final.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@JelleZijlstraJelleZijlstraJelleZijlstra left review comments

@erictrauterictrauterictraut left review comments

@hauntsaninjahauntsaninjahauntsaninja approved these changes

Assignees
No one assigned
Labels
topic: typing specFor improving the typing spec
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

5 participants
@samwgoldman@erictraut@hauntsaninja@JelleZijlstra@srittau

[8]ページ先頭

©2009-2025 Movatter.jp