cattrs icon indicating copy to clipboard operation
cattrs copied to clipboard

determine a way to work in a world where Unions don't exist any more

Open glyph opened this issue 7 years ago • 1 comments

This might be related to #37 . (Fortuitous ticket number there.)

Recent versions of MyPy emit errors if you try to use a Union at all at runtime, which appears to be a strong signal that cattrs's approach to serialization may be at odds with future plans for the typing module.

I've filed this upstream for discussion at https://github.com/python/mypy/issues/5354 but it might be worthwhile to start figuring out if there's a way to do this that is compatible with the future that is implied by this change.

glyph avatar Jul 13 '18 23:07 glyph

Thanks for bringing this to my attention.

Tinche avatar Jul 14 '18 02:07 Tinche

So I'm bored and going over ancient cattrs issues to see what I can weed out.

The main issue here is that Mypy won't let you use a union in a typevar, right? I think Guido has actually expressed a willingness to change this a few months ago, so when I get myself some more Mypy clout I'll go over there and beg them to actually put this in.

As for a future without unions, my personal preference over time has shifted to sum types quite strongly. I'd be sad if unions were phased out :(

Tinche avatar Jul 08 '23 00:07 Tinche

The main issue here is that Mypy won't let you use a union in a typevar, right?

I've never wanted to use a union as a TypeVar type, only as a bound, and that's allowed.

I think Guido has actually expressed a willingness to change this a few months ago, so when I get myself some more Mypy clout I'll go over there and beg them to actually put this in.

Luckily it's already in, and has been in a really long time. It was fixed in 0.780.

https://mypy-play.net/?mypy=0.770&python=3.11&flags=strict&gist=884709b80f869f50ebf7ddb32c0ae79e

https://mypy-play.net/?mypy=0.780&python=3.11&flags=strict&gist=59145a555f1dd45f2900ba1792393ef5

As for a future without unions, my personal preference over time has shifted to sum types quite strongly. I'd be sad if unions were phased out :(

Sum types are fine, this was just about metaprogramming, and there will always be some kind of workaround. Although apparently we don't need one any more :)

glyph avatar Jul 10 '23 04:07 glyph

Ah sorry, I meant that type[T] doesn't bind to a union: https://mypy-play.net/?mypy=latest&python=3.11&flags=strict&gist=085e358a161c48939c6d8b9996b6e487

A core cattrs thing is:

from cattrs import structure

a = structure("whatever", str | int)
# `a: str | int` now

but that hasn't worked since forever. I think we're talking about different things though.

Edit: fixed the link

Tinche avatar Jul 11 '23 10:07 Tinche

but that hasn't worked since forever. I think we're talking about different things though.

Ah, yes. That's annoying. They're related things, in that this used to be an error and now it's merely not helpful.

You can sort of trick mypy into this though, with syntax like

structure[int | str].from_value("x")

which, handwaving over oodles of code mangling __getattr__ into actually remembering the type object, you can implement like this:

from typing import TypeVar, Generic, cast, reveal_type
T = TypeVar("T")
class structure(Generic[T]):
    @classmethod
    def from_value(cls, value: object) -> T:
        return cast(T, None)
val = structure[int | str].from_value("x")
reveal_type(val)

glyph avatar Jul 12 '23 04:07 glyph