-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Sibling subclasses (without stubs) pass for each other? #3987
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
Comments
How are you running mypy? In particular are you using --follow-imports=silent? Maybe the base classes (*TensorBase) aren't found. |
The first output was with It looks like there are a lot of problems with things that look like *TensorBase:
and
If it doesn't know enough about their base class(es), would mypy assume that they could be compatible? |
Yeah, that's likely what's causing this. Try experimenting with a small example. |
I experimented a bit but couldn't determine more than the above. The surface question I had was whether mypy assumes that objects with somewhat ambiguous base classes are compatible. The answer to this seems to be "yes." It would be nice if The deeper question I had was how I can fix this. I was hoping that there's a way to gradually add type annotations without undergoing the behemoth task of documenting types for a big (and rapidly changing) 3rd party library like pytorch. I checked the docs and the wiki sections about I guess the tl;dr is: For really big projects, mypy gets a lot of it right, but some of it wrong. It would be nice to be able to tell (a) how it's getting things wrong, (b) fix these problems when they arise without trying to document big swaths of the project. I'm hoping to ask if either of these are possible! |
Could you run stubgen over pytorch? Agreed that for really big projects it's a commitment. Zulip, approximately 100k LoC, did it over a summer with one dedicated GSoC student. At Dropbox we have maybe 5M LoC, or which approximately 20% is annotated. It's taken us a year and a half to get there, with a dedicated team of several (of course we also had to improve mypy itself a lot to get there, so you can do it for less :-). |
stubgen seems promising! Running vanilla To try to get the remaining modules, I ran Running on the code I posted in the top of this issue: $ mypy mypy_minimal_example.py
out/torch/__init__.pyi:5: error: Cannot find module named '_C'
out/torch/__init__.pyi:5: note: (Perhaps setting MYPYPATH or using the "--ignore-missing-imports" flag would help)
out/torch/_C.pyi:2691: error: invalid syntax I'm guessing that the ordering of the errors is simply the confusing part here, and it can't find I'm not sure what the syntax error is here? (And is it a bug if stubgen generated files with syntax errors?) |
@mbforbes Can you try this with the |
@ilevkivskyi thanks for the quick tip! Running from master removes the first error, but not the supposed syntax error: $ mypy --version
mypy 0.530-dev-c332ea08b542728985fadcbee1db6651060c3876
$ mypy mypy_minimal_example.py
out/torch/_C.pyi:2691: error: invalid syntax Line 2691 is the same as above. (I am happy to provide more info if that would be helpful.) |
Ah, |
@mbforbes If this is a stub for C extension module (my guess), I think you could just rename the argument to |
I was thinking that might be the issue... that is a little bothersome because it's a 3rd party repository, and they do label that as the paramter name; I assume since python runs their code, they're using a C extension; in that case, could / should stubgen replace keywords like Fixing the ...
def addbmm(beta=1, mat, alpha=1, batch1, batch2, out=None): ...
def addcdiv(tensor, value=1, tensor1, tensor2, out=None): ...
def addcmul(tensor, value=1, tensor1, tensor2, out=None): ...
... At this point I'm actually not sure what side of the fence this problem lands on. On one hand, this is code that runs, so it seems like stubgen ought to be robust to it. And I can see the draw from (their) API perspective to mix default and non-default arguments like this. On the other hand, maybe it's not stubgen's responsibility to fix up syntactically incorrect Python interfaces. |
Just because their docs show
Again I think you are taking their docs a bit too literally.
You keep using that phrase, but clearly the docs don't run, and the implementation isn't written in Python. So keep it with a grain of salt.
Please understand that stubgen is a tool to help you create stub files, but it's not as robust as mypy itself, and it isn't maintained that much. You're supposed to manually review the stubs (as well as test them) before you start using them. If you find a bug in stubgen you're welcome to report an issue but we may not prioritize fixing it. If you submit a PR we may review and merge it, assuming you're not making sweeping code changes (try to refrain from a big refactor). |
Thanks for the insight into what's going on here. I think I'm still new enough to C extensions that I have a flawed understanding of how the docs / C / Python interact. I went down the path of fixing some more of the problems mypy found in what stubgen created from torch, but I quickly realized this was going to become exactly the undertaking I was hoping to avoid. In order to get some of the fundamental types (e.g., I greatly appreciate the help above. I'm going to open one more issue that I've had in trying to go back to running stub-less, but I totally get it if I'm treading in unsupported waters at this point. |
In what is perhaps a fool's errand, I'm hoping to use mypy to help typecheck code written using a big library without stubs (pytorch).
mypy seems to know something about the types of the variables, but not quite enough to be helpful. I'm hoping to understand why the below happens and if there's anything I can do about it.
Code:
Output of
mypy --follow-imports=silent
:I am sorry if this is because of my confusion with python or mypy in general.
More info:
The definitions of the above classes:
The text was updated successfully, but these errors were encountered: