[Python-ideas] Trial balloon: adding variable type declarations in support of PEP 484

Eric Snow ericsnowcurrently at gmail.com
Thu Aug 11 13:01:04 EDT 2016


On Tue, Aug 9, 2016 at 5:32 PM, Eric Snow <ericsnowcurrently at gmail.com> wrote:
> There are a number of options here for identifying attributes
> in a definition and even auto-generating parts of the class (e.g.
> __init__).  Let's look at several (with various objectives):
>
> # currently (with comments for type info)
> # using attribute annotations and a decorator (and PEP 520)
> # using attribute annotations and a metaclass (and PEP 520)
> # using one class decorator and PEP 520 and comments for type info
> # using one class decorator and comments for type info
> # using one class decorator (and PEP 468) and comments for type info
> # using a class decorator for each attribute

Another approach that I've used in the past (along with a derivative):

# using a non-data descriptor

@as_namedtuple
class Bee:
    """..."""

    name = Attr(str, 'Eric', doc='the bee's name')
    ancient_injury = Attr(bool, False)
    menagerie = Attr(bool, False)

    def half_a(self): ...


# using a non-data descriptor along with PEP 487

class Bee(Namedtuple):
    """..."""

    name = Attr(str, 'Eric', doc='the bee's name')
    ancient_injury = Attr(bool, False)
    menagerie = Attr(bool, False)

    def half_a(self): ...

While the descriptor gives you docstrings (a la property), I expect it
isn't as static-analysis-friendly as the proposed variable
annotations.

-eric


More information about the Python-ideas mailing list