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

Add an Intersection type #181

Open
JelleZijlstra opened this issue May 12, 2021 · 0 comments
Open

Add an Intersection type #181

JelleZijlstra opened this issue May 12, 2021 · 0 comments

Comments

@JelleZijlstra
Copy link
Contributor

I promised in python/typing#213 that I'd add support for Intersection, so I'd better do it. I'd like to get Protocol support (#163) in first, though, because most realistic use cases for Intersection involve protocols. Here's a sketch of how the implementation could work:

  • Add pyanalyze.extensions.Intersection, which would work similar to Union at runtime. Intersection[int, str] means a value which is both an int and a str. We should also support int & str in string annotations, similar to the existing support for int | str.
  • Add a new IntersectionValue to value.py and handle it in lots of places. For example:
    • IntersectionValue.can_assign(val) would check that val is assignable to all members of the intersection
    • val.can_assign(intersectionValue) would succeed if any member of the intersection is assignable to val
    • Getting an attribute on an IntersectionValue should return something if any member of the intersection has the attribute
      • What if they have conflicting values? I guess we can return another Intersection
  • Find ways to use Intersection internally. For example, various narrowing operations could return Intersection objects.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant