Attrs: Bunch of typing issues

Created on 5 Jun 2018  Â·  12Comments  Â·  Source: python-attrs/attrs

So this is mostly a meta ticket in which I ask @chadrik and @euresti (others of course too :)) for help to tell me where to direct them:

  • [ ] partials don’t work as factories: error: No overload variant of "Factory" matches argument types [functools.partial[Any]]
  • [x] classes don’t work as converters which is a bummer because I love using yarl.URL as converter. :)
  • [x] Having an attribute with a default and init=False makes it impossible to add more attributes. Like this:
    ```python
    import attr

@attr.s
class C:
x = attr.ib(init=False, default=42)
y = attr.ib()
```

It works perfectly fine to run but mypy will complain: error: Non-default attributes not allowed after default attributes.

Minor:

  • lambdas don't work as converters on purpose I suppose?
Bug

All 12 comments

  • partials don’t work as factories: error: No overload variant of "Factory" matches argument types [functools.partial[Any]]

Stubs issue (i.e. typeshed) Though I'm a little surprised that it doesn't already work.

  • classes don’t work as converters which is a bummer because I love using yarl.URL as converter. :)

Hmm. This one is supposed to work in the plugin. What error do you get? Which version of mypy? Can you post some code?

  • Having an attribute with a default and init=False makes it impossible to add more attributes.

Plugin issue. Not something I handle correctly.

  • lambdas don't work as converters on purpose I suppose?

Plugin issue. Not so much on purpose as in they are fairly difficult to implement in mypy.

Feel free to file mypy bugs for the plugin issues, and typeshed bugs for the Factory issue.

import attr
import yarl

@attr.s
class C:
    url = attr.ib(converter=yarl.URL)

gives me: error: Unsupported converter function, only named functions are currently supported on mypy 0.600.

Can you try replacing yarl.URL with another class? One that has type
annotations? Something in click, yaml or attrs?

On Tue, Jun 5, 2018 at 6:31 AM, Hynek Schlawack notifications@github.com
wrote:

import attrimport yarl
@attr.sclass C:
url = attr.ib(converter=yarl.URL)

gives me: error: Unsupported converter function, only named functions are
currently supported on mypy 0.600.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/python-attrs/attrs/issues/389#issuecomment-394709846,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABCCAgkLqYGTA_KX00OJhDOx0txOewK2ks5t5og5gaJpZM4UarmJ
.

yarl actually has type annotations: https://github.com/aio-libs/yarl/blob/master/yarl/__init__.pyi#L6

It was the first pkg I ran into that shipped them. :)

(I tried an attrs class and the result was the same. Given the error message, it doesn’t look like it even looks at the types.)

Ah it looks like they haven't released in a bit. It works correctly on
mypy master.

On Tue, Jun 5, 2018 at 6:38 AM, Hynek Schlawack notifications@github.com
wrote:

(I tried an attrs class and the result was the same. Given the error
message, it doesn’t look like it even looks at the types.)

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/python-attrs/attrs/issues/389#issuecomment-394712221,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABCCAl8nSjUB6d-BLjsccSEZoM5vjCt9ks5t5onfgaJpZM4UarmJ
.

Cool! Will enums work too? Just ran into it and it’s super useful. :)

Sure looks like it does:


import attr
from enum import Enum

class Em(Enum):
    x = 'x'
    y = 'y'

@attr.s
class C:
    url: Em = attr.ib(converter=Em)

On Tue, Jun 5, 2018 at 7:18 AM, Hynek Schlawack notifications@github.com
wrote:

Cool! Will enums work too? Just ran into it and it’s super useful. :)

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/python-attrs/attrs/issues/389#issuecomment-394726713,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABCCAn6ojf8Mj8Itwijo7BiYYRe0KbZXks5t5pMpgaJpZM4UarmJ
.

So it’s basically just the partial thing. We should try to close #238 ASAP because right now I’m not sure where to report.

  • mypy 0.620 has fixed the init=False thing.
  • it looks like
from functools import partial
import attr

f = partial(int, 42)

@attr.s
class C:
   x = attr.ib(factory=f)

works too now…I didn’t see anything in changelogs, but it doesn’t just work by accident, does it?

init=False fixed here: https://github.com/python/mypy/pull/5154

git bisect tells me the other one was fixed by this https://github.com/python/mypy/pull/5084
Something with how overloads are handled.

lambdas still won't work as converters though.

Coolio!

Was this page helpful?
0 / 5 - 0 ratings