Skip to content

Equivalence between similar-looking types #6

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

Closed
gvanrossum opened this issue Oct 15, 2014 · 4 comments
Closed

Equivalence between similar-looking types #6

gvanrossum opened this issue Oct 15, 2014 · 4 comments

Comments

@gvanrossum
Copy link
Member

Lukasz asks a series of questions about type equivalence.

  • Set is not Set[Employee]. Correct!
  • Set != Set[Employee]. (He didn't ask this, but I'm inserting it in the list because it's a useful question to ask.) Correct again.
  • Set is Set[Any]. No. I'm tempted to make these different objects, even if they have the same meaning, just so that we can have G[t1][t2] == G[t1, t2] for any generic type taking two arguments. For example a dict with string keys but unspecified values could be written as Dict[str] or Dict[str, Any], but only the first form could be further parametrized with a value type, so Dict[str][int] is equivalent to Dict[str, int].
  • Set == Set[Any]. Again, no, since these behave differently.
  • Set[Employee] is Set[Employee]. I think not. Given that the is operator means object identity, this would require some kind of caching of all parametrized type objects, and I'd like to avoid that.
  • Set[Employee] == Set[employee]. Yes. (The cat should be out of the bag by now. I am proposing an eq method on generic type objects to make this true.)
  • issubclass(Set[Employee], Set). Hm... This reeks of the covariance/contravariance discussion (issue Covariance or contravariance #2). But for the special case of a parametrizable type and a parametrized version of that type I think this is reasonable.
  • issubclass(Set[Manager], Set[Employee]). See issue Covariance or contravariance #2.
@ambv
Copy link
Contributor

ambv commented Jan 7, 2015

What does it mean that Set != Set[Any]?

I would also like if Dict[str] was invalid (type checker error) and Dict[str, Any] or Dict[str, ConcreteType] was mandatory. One obvious way to do it. Especially that we overload [] quite a lot in this proposal, with Unions, Callables and Tuples further complicating the issue.

@gvanrossum
Copy link
Member Author

Set != Set[Any] because Set[Any] cannot be further parametrized (Any is not a type variable) while Set can be.

Also, even if we decide that subclassing is invariant so Set[Manager] is not a subclass of Set[Employee](nor vice versa), I would still like to be able to see both being a subclass of (unparametrized) Set.

Agreed that Dict[str] should be illegal (and it is, both in mypy and in my prototype).

@ambv ambv self-assigned this Jan 8, 2015
@ambv
Copy link
Contributor

ambv commented Jan 8, 2015

OK, will update the PEP accordingly.

@gvanrossum
Copy link
Member Author

I don't think there's anything left to do here. Closing.

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

2 participants