Union should work for constrained type variable #1911
Unanswered
hannes-ucsc
asked this question in
Q&A
Replies: 1 comment
-
If you want to be able to pass in an union you should use an upper bound of It's rare that you actually need a constrained type variable. They're not very well specced out currently, so you can expect many bugs and weird corner cases. def truncate[S: str | bytes](s: S) -> S:
return s[:10]
def log(v: str | bytes):
print(truncate(v)) I see that this version leads to a different error in mypy for |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
https://mypy-play.net/?mypy=master&python=3.12&gist=f0db7bce0a6d929cff485bd50c76a158
How can I avoid the error without narrowing to every possible alternative of the union (note how the
if
andelse
branches are identical) …… or adding overloads to
truncate
? Both seem like inelegant and tedious workarounds.Beta Was this translation helpful? Give feedback.
All reactions