A few issues with Maybe #3981
Unanswered
Corentin-Bravo
asked this question in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I've started migrating my application from using UNSET to using Maybe only.
I met a few annoyances which I thought would be nice to be discussed before creating issues. I put them in a rough order of how much I care about.
It should be
Maybe[T]: TypeAlias = Union[Some[T], None]
so that Maybe is considered a Generic for mypy.Why that matters?
Will not be accepted by mypy with the current implementation (as of mypy 1.16.1 and strawberry 0.280).
error: Type application is only supported for generic classes [misc]
I'm fairly certain that it was explicitly intended that this should not happen. Was I wrong?
datetime_field
the input will not work.If I want the input to work as expected I must do:
Will have mypy complaining with:
error: Missing named argument "attr" for "MyInput"
mypy will complain with
Argument 1 to "call_some" has incompatible type "Some[int | None]"; expected "Some[int] | Some[None]"
It's slightly frustrating, because Some[int] | Some[None] is basically the same as Some[int | None].
I could see it being a "feature" to encourage people to unwrap their Some within the route and not propagate them everywhere in the code.
But I found it slightly frustrating so I thought I'd point it out.
I used to have functions that took in inputs some values (that could be T, None or UnsetType) and did things depending on them.
Those functions cannot work anymore because None without context is ambiguous. The function has no way to know if it's a None from a Maybe, a None from a Some[StrawberryOptional] or a None from a StrawberryOptional.
For example
Using Some does not work well due to the above type-checking issue (You need to have an overload for Some[str], Some[str | None] and you'll want an overload for Some[None] just for completeness, which is a bit too much boilerplate).
Plus considering the Create inputs is likely to NOT have Maybe, while the Update inputs does, it becomes very messy.
I worked around it by creating a partial TypedDict, which helps handling the difference between "undefined" and None. But it lead to less factorization of code, because I have to be careful with Maybe (unwrapping and rewrapping them when needed, and skipping the correct None)
While this is a compilation of my complaints, I will say that it's much more elegant than my solution, and the disappearance of Unset makes the type checking much better overall (with the only slight drawback as I said that the developper must be much more careful around None).
Beta Was this translation helpful? Give feedback.
All reactions