Skip to content
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

Join doesn't treat correctly variance of generics #5269

Closed
ilevkivskyi opened this issue Jun 24, 2018 · 2 comments · Fixed by #9994
Closed

Join doesn't treat correctly variance of generics #5269

ilevkivskyi opened this issue Jun 24, 2018 · 2 comments · Fixed by #9994
Labels
bug mypy got something wrong priority-1-normal

Comments

@ilevkivskyi
Copy link
Member

ilevkivskyi commented Jun 24, 2018

Currently, joins for the instances of same type are calculated using this code:

        if is_subtype(t, s) or is_subtype(s, t):
            args = []
            for i in range(len(t.args)):
                args.append(join_types(t.args[i], s.args[i]))
            return Instance(t.type, args)

This has two problems:

  • For a covariant generic G, the join of G[int] and G[str], is currently just object, not G[object] as it (I believe) should be.
  • This behaviour is wrong for contravariant types, in this case there should be a meet of arguments: so that join(C[int], C[float]) should be C[int] for contravariant C, while currently it is C[float].

A correct behaviour would to check for every type argument position:

  • if the class is covariant in it, calculate join of args on the left and right
  • if the class is contravariant in it, calculate meet of args
  • if the class is invariant in it, check that the type on the left and right are the same
  • if the last one didn't hold, fall back to object.
  • verify bounds and values restrictions for new args, if some are not satisfied, fall back to object.
  • (optional) if one of the meets in third bullet produced an UninhabitedType, fall back to object as well.
@ilevkivskyi
Copy link
Member Author

An additional important step is to verify bounds and values before creating a new instance. I edited the description.

@ilevkivskyi
Copy link
Member Author

#4170 provides a real-world example of this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-1-normal
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant