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

[stubtest] Support @final decorator on methods as well #14924

Open
sobolevn opened this issue Mar 19, 2023 · 2 comments
Open

[stubtest] Support @final decorator on methods as well #14924

sobolevn opened this issue Mar 19, 2023 · 2 comments

Comments

@sobolevn
Copy link
Member

After #14922 is merged, I think we can also add a check that this source code has correct stubs:

class A:
    @final
    def method(self): ...

Right now stubs do not have to add @final decorator, stubtest will allow both versions.

@sobolevn
Copy link
Member Author

After working on for a short time, I've noticed several problems:

  1. @property is not supported
  2. @staticmethod is not supported
  3. @classmethod is not supported

This happens, because __final__ cannot be set on property objects:

See:

>>> def my_final(f):
...    f.__final__ = True
...    return f
>>> class P:
...    @my_final
...    @property
...    def p(self) -> int: ...
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in P
  File "<stdin>", line 2, in my_final
AttributeError: 'property' object has no attribute '__final__'

But, @classmethod and @staticmethod actually change the object when using getattr, this is why this happens:

>>> class St:
...   @my_final
...   @staticmethod
...   def m() -> None: ...
... 
>>> St.m
<function St.m at 0x109eeb400>
>>> St.m.__final__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute '__final__'. Did you mean: '__init__'?

>>> St.__dict__['m']
<staticmethod(<function St.m at 0x109eeb400>)>
>>> St.__dict__['m'].__final__
True

This needs:

  1. Special-casing properties. They cannot be supported right now
  2. Magic getattr_static to get methods from the class

This is quite complicated for a Sunday's evening project, so - please feel free to continue it if someone finds it useful.

@sobolevn sobolevn removed their assignment Mar 19, 2023
sobolevn pushed a commit that referenced this issue Mar 24, 2023
…l` if they are decorated with `@final` at runtime (#14951)

This implements most of #14924. The only thing it _doesn't_ implement is
verification for overloaded methods decorated with `@final` -- I tried
working on that, but hit #14950.
@AlexWaygood
Copy link
Member

To update on the status here: following #14951, this is now fully implemented except for cases where the method is overloaded in the stub (implementing that is blocked by #14950).

@AlexWaygood AlexWaygood added the topic-final PEP 591 label Mar 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants