-
-
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
We need a better story about Any #3194
Comments
Hm, I have to think about this. I'm not sure I agree with everything in the first diagram either. I thought joins with Any always gave Any? (IOW why is rule (2) not the same as rule (4)?) Off-topic there's also something with |
The reasoning why I don't like this is similar to why we recently prohibited simplifying
Rules (2) and (5) are different form others because I want to preserve the fact that |
OK, that explains (1), (2), (5) and (6) to me. That leaves (3) and (4). I think you're saying (3) It seems the right way to think about the lattice is that up (join) is the direction of fewer methods and more values (with fewer methods being tested first), while down (meet) is the direction of fewer values and more methods (with fewer values being tested first). PS. Please have pity on my feeble understanding. I have never had any theory classes about this at all, since except for Simula, OO was invented after I graduated! |
The join with
I think that both calls should be accepted, since each could be okay, given a suitable precise type for |
So you disagree with (1) and (3) from the OP?
|
Of course, a user could always overrule the inference with an explicit annotation, but it seems to me it is better to silence an error with |
@JukkaL def g1(a: Sequence[A]) -> None:
... You could argue that user-defined classes are invariant by default, we already have a section in "Common issues" about this, so that users will be prepared that sometimes explicit annotations are needed. |
I may well disagree with the rules for meet as well, but this needs some thought as the issue is subtle so I'm focusing on join first. By the way, I'm not convinced that this is analogous to the union simplification fix and this needs to argued for based on its merits. One of the reasons why class A:
def f(self) -> None: pass
class B: pass
def f(a: A, b: Any) -> None:
x = [a, b]
x[1].f() # unsafe if b is an instance of B, for example With the current semantics, the receiver object on the final line has type |
Another issue with the rules for joins is the behavior with def f(a: str, b: Any) -> None:
x = [a, b][0]
if isinstance(x, str):
...
else:
x(1 + '') # should be an error If we infer Actually, it looks like inferring |
But the only way for a user to see this is to use def f(a: A, b: Any) -> None:
x = [a, b]
x[1].f()
x[1].never_had_this()
if function_that_typically_returns_zero():
x[0].this_will_fail_late()
This example is interesting. Inferring |
I don't think this is an issue that should be resolved in isolation. We have a more thorough review of the type system on our roadmap for the next couple months -- we should make sure to consider the role of Any as part of that, but doing it beforehand would be duplicating work and could potentially lead us in the wrong direction. |
Ah, thanks, I didn't know this. Then I propose to postpone this discussion, but leave the issue open just to be sure we will not forget about this. |
I wonder if the review should be done in public rather than as Dropbox meetings. |
I can't say for others, but I would very much like it if I had the chance to join :-) |
By the way, is this roadmap a secret document, or is it available somewhere? |
Our current roadmap for mypy is not public, but at least I wouldn't mind making it public -- with the caveat that it's continuously evolving. |
Jukka, I think it's a great idea to publish the internal road map (I can't even recall what's on it). Maybe we can put it in a file named ROADMAP.md at the top level of the tree? Alternatively it could be a chapter in the docs. Another approach would be to create issues for each of the items and tie them together with a custom label (or, dare I say, a milestone?). We should recall that road maps are often vague and subject to change -- an item on the road map doesn't promise it will be delivered by a certain date. |
How about putting it in the wiki? Just restrict editing to collaborators only. |
We're actually trying to kill the wiki, it's full of unmaintained stuff. |
FWIW I agree that the Developer Guides section is probably mostly crap at this point, but I don't really see why it wouldn't be an ideal place for a roadmap. It would have dead-easy editing from either the command line or the GUI, too. Also: http://www.mypy-lang.org/roadmap.html This really needs to be updated:
|
@JukkaL -- thoughts about the wiki and the outdated road map? @kirbyfan64 -- that website is in GitHub, you can send PRs here: https://github.com/JukkaL/mypy-website |
To get the discussion moving on, here is how the type system behaves currently:
If I were to change anything, I'd consider changing meets (this is just an idea, I haven't carefully considered all the implications):
And here is how I think about
|
I think we should add the
The rules for join/meet should satisfy their definitions i.e. I would propose to add a fifth rule to how to think about
This rule already holds. I would propose to keep it, so that |
While working on protocols, I noticed that stubs often use
Any
whereobject
will be more appropriate and vice versa. Because of how protocol inference works and how current mypy solver works, this leads to problems since sometimes mypy infersAny
instead of giving an error (maybe we could improve this by "tightening" solver?)I think the root of the problem is that wee don't have a good story about
Any
. I believe the rules for interaction betweenAny
and other things should be like this (whereC
is a normal class likeint
orstr
):I use
NoReturn
for uninhabited type, since it is how it is currently called in PEP 484. The above rules could be summarized in a simple diagram:Currently, mypy behaves in a completely different way, and I believe this is not right, let me explain why. We could focus on rules (3) and (4). Currently, mypy returns
Any
in both these cases. Here is why it is not safe:@JukkaL I will be grateful for your opinion.
The text was updated successfully, but these errors were encountered: