-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use union instead of join for unifying types #12056
Comments
Nevermind: I was being confused. This doesn't matter I believe. (TypeForm is more about passing a type in, not about the type of an object) |
Undoes python#5095. Part of python#12056. This fails a few tests but I want to see the mypy-primer output before I spend time fixing them.
I agree that we should probably do this, but I also want to see the fallout first. Due to our backward compatibility policy, the change needs to be first introduced behind a feature flag, and we need a major mypy release when we switch the default (e.g. 2.0). This will let us iterate on the change and allow users to experiment with it easily before we make the switch. |
Just to add my 50 cents here: I don't like the idea of completely switching to unions. The fact that this will fix 20 issues doesn't guarantee that we will not get 40 new issues caused by things like: def basket() -> set[Fruit]:
res = {Banana(), Apple(), Orange()}
# do something with res...
return res I think a better (and probably safer) approach is: we should infer unions in "heterogeneous" contexts, while keeping joins in "homogeneous" contexts. By "heterogeneous" contexts I mean things like tuple types, ternary expressions, user declared unions, star arguments (which are also tuples under the hood), maybe there are some more. Note that a significant amount of issues mentioned above are about these cases. We can try switching these cases one by one, and see what is the fallout. My prediction is that fallout from switching tuple fallback to union will be minimal (in particular since tuple instance is covariant), we can start with this. |
@ilevkivskyi In my experience, we haven't seen any issues with this functionality in basedmypy. You even get a very useful message in the example you provided: class Fruit: ...
class Apple(Fruit): ...
class Orange(Fruit): ...
class Banana(Fruit): ...
def f() -> set[Fruit]:
result = {Apple(), Orange(), Banana()}
return result # test.py:26: error: Incompatible return value type (got "set[Apple | Orange | Banana]", expected "set[Fruit]") [return-value]
# test.py:26: note: Perhaps you need a type annotation for "a"? Suggestion: "set[Fruit]" Just some opinions from the basedmypy team is that TypeScript always performs a unionization here as well. |
Ref #12056 If `mypy_primer` will look good, I will add some logic to shorted unions in error messages. cc @JukkaL --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Alex Waygood <[email protected]>
because I'm curious -- is there any trick to adjust mypy's inference to resolve to the union that's shorter than enumerating all the types? class Base: pass
class A(Base): pass
class B(Base): pass
class C(Base): pass
x = [A, B, C] # implicitly `list[type[Base]]`
y: list[type[A | B | C]] = [A, B, C] # works but is a little cumbersome as the number increases |
I am actually going to add a flag so that people can opt in if they want more unions (the default however is unlikely to change in next few years). |
@ilevkivskyi what's the best way to express support for the union behavior? Also, judging by your phrasing, may I assume that mypy may get the "union way" under as an opt-in sooner rather than later? |
@erictraut has gathered a long list of problems caused by mypy's behavior of using the "join" operator to unify types in various contexts.
A | B | None
becomesobject
after narrowing checks #12009object
not a union #11440I believe that we should change this behavior.
If someone opens a PR to do this, it would be interesting to see what the results look like in mypy-primer and mypy's CI.
The text was updated successfully, but these errors were encountered: