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

Support "x = property(...)" in class body #8083

Open
JukkaL opened this issue Dec 5, 2019 · 3 comments
Open

Support "x = property(...)" in class body #8083

JukkaL opened this issue Dec 5, 2019 · 3 comments
Labels
false-positive mypy gave an error on correct code feature priority-1-normal topic-descriptors Properties, class vs. instance attributes

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Dec 5, 2019

Currently it's not possible to define a property using x = property(...) in the class body.

(Originally from #220.)

@adiroiban
Copy link

adiroiban commented Jun 20, 2020

I am not sure if this should be a separate issue.. I couldn't find the issue for properties outside of class body..

I am leaving this comment here, maybe it will help with testing of a future mypy implementation

def WritableNumber(name, default=None):
    """
    This is a generator of properties.

    Number configuration which can be written.

    A number is an integer or a float. It can be negative.
    """
    def getter(self):
        result = self._proxy.getString(self._section_name, name)
        if not result and default is not None:
            return default

        result = self._proxy.getFloat(self._section_name, name)
        if not result and default is not None:
            return default

        return result

    def setter(self, value):
        return self._proxy.setFloat(self._section_name, name, value)

    return property(getter, setter)

class MyConfig(object):
    """Class using the property generator. The class will initiate the `_proxy` member."""
    some_value = WritableNumber('config_name', default=1)

I see that you can define properties outside of a class body.

Python 3.8.2 (default, Apr 27 2020, 15:53:34) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> ca = property(lambda _: 1)
>>> class A:
...   a = ca
... 
>>> a = A()
>>> a.a
1 

@IgnacioJPickering
Copy link

Is there any known workaround for this?

@rmorshea
Copy link

@IgnacioJPickering see: #8440 (comment)

@AlexWaygood AlexWaygood added the topic-descriptors Properties, class vs. instance attributes label Aug 25, 2023
chrysn added a commit to chrysn/aiocoap that referenced this issue May 12, 2024
This includes one actual bugfix found by mypy: Group setups where one
algorithm was None (eg. no signing mode supported) would have raised an
exception trying to access `None.value`.

Includes-Workaround-For: python/mypy#8083
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false-positive mypy gave an error on correct code feature priority-1-normal topic-descriptors Properties, class vs. instance attributes
Projects
None yet
Development

No branches or pull requests

5 participants