-
Notifications
You must be signed in to change notification settings - Fork 91
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
Type annotations? #599
Comments
I have been thinking about this after dropping support for 2.7, but didn't get to it yet. |
- includes FakeFileOpen and all file wrapper classes - see pytest-dev#599
- includes FakeFileOpen and all file wrapper classes - see #599
…wrapper classes - see #599
@mrbean-bremen looks like you have added more type annotations! At what point do you think that it makes sense to include a |
If we puy that in, it would just check the annotated modules, correct? In this case we could do it now - I have to admit that I wasn't aware that this is needed for mypy. |
Yes, I think it would treat anything that is not annotated as |
Agreed. I think we will not publish a new release until the relevant modules are typed, so this will probably not be a problem. This may take a while, as I'm working on this only every now and then, but I think there is no rush. |
I imported pyfakefs 5.0.0 to our source base at Google, and had difficulties with the new typing annotations when running pytype (https://pypi.org/project/pytype/). In particular, I made the following local edit:
Have the changes been tested on pytype in addition to mypy? This is a critical issue for our code base, as we have required pytype for all of our new code since 2021. I also noticed issues running pytype on the unit tests in the tests/ directory. Thanks! |
Thanks - no, so far we haven't used pytype, though it could easily get integrated into the CI. I will have a look at pytype tonight or later this week and see if I can have another go. As always, any help is appreciated :) |
I haven't used And good to know that
is still valid... I guess that you haven't used a current version until now, as the typing stuff is in there for some time. The 5.0.0 release was mostly related to the move to |
Thanks for taking a look at pytype. Yes, pyfakefs is still very much a part of Google's code base. "Over 2,000" is an accurate count. As Google's pyfakefs maintainer, I've had to do some interesting large scale code updates to keep up with pyfakefs changes, and only recently decided to surface this challenge and one I created another issue for. Google still standardizes on unittest rather than pytest, though we do support running pytest, especially for PyPi packages. |
By the way, I also really appreciate and give kudos for the continued support and added features of pyfakefs from the community, especially in keeping up with new Python versions and new features like type annotations! |
Glad to hear that pyfakefs is still used, and that you continue to maintain it at Google! Interesting - by which changes in pyfakefs have these large scale code updates been triggered? Regarding unittest vs pytest: Out of interest, I recently checked how pyfakefs is used in the repos depending on it (I checked only ones these that have at least 100 stars). Of these (54 repos), 24 use it with unittest, 23 with pytest, and the rest with Patcher or by manually patching the classes. So it is still a tie between unittest and pytest, though pytest is on the rise... |
Requiring unit tests based on unittest predates my joining Google 12 year ago. I'm still leading an effort to deprecate the pyfakefs PascalCase function names. Two thousand unit test files is a low figure. |
I guess that was in large part due to Mike Bland and Co.
Ok... I removed the PascalCase in version 5 because I haven't seen it used anywhere, and it had been deprecated for almost 5 years now, so I thought that we are on the save side here. Alas...
It is relatively, and replacing the API is probably mostly search and replace, but it is still quite some work... |
This may take a while... |
Thanks for looking deeper into the problem. I provided my workaround, though admittedly, it is just turning off type checking for particular signatures to allow pytest to pass. This helps our code base, so that we can apply pytest checks to our code. I saw some other functions that expect differing types for args, and I found that the @typing.overload decorator helps in those situations:. |
Thank you - yes, I have been using overloads in some places. Maybe I have to add more of them, though I remember that I had tried this at the time and didn't get far. I will see what I can do, maybe I have to relax some checks a bit. |
Just to let you know: I have been tinkering with this issue (e.g. the We are happy to accept PRs that fix the problem, or add a workaround for |
Hi, thanks for trying to figure out the problem with the annotations. Could you please add the details to reproduce the error in pyfakefs at https://github.com/google/pytype/issues/new, so that the pytype owners can look at why the annotations won't work in both tools? Thanks. |
Will do this, though I probably need to first boil this down to some reproducible case. This may actually help me to understand this better myself. I will try to do this over the weekend. |
I'm still not sure if I just using this wrong. Here is boiled down example code from import os
from typing import Union, Optional, AnyStr, overload, cast
AnyString = Union[str, bytes]
AnyPath = Union[AnyString, os.PathLike]
@overload
def make_string_path(dir_name: AnyStr) -> AnyStr:
...
@overload
def make_string_path(dir_name: os.PathLike) -> str:
...
# @overload
def make_string_path(dir_name: AnyPath) -> AnyString:
return cast(AnyString, os.fspath(dir_name)) As it is, it works with mypy, but shows an error with
This can be fixed by commenting in the |
My read of |
Also, I have not had to make use of |
Agreed. I had just been playing around and noticed that setting that overload seems to satisfy
Yes, I'm aware of this. I sometimes need this because the return type of a Python library function is incorrectly inferred. Anyway, I will probably make a restart with this some time later - I think I got stuck in the details and lost the overall picture, and I may have to read up on typing. |
As an update to The workaround of replacing the |
Another error found, but can be resolved with
|
Thanks - I will check this. |
I'm going to close this now that the As for |
I'm trying to use pyfakefs on a project that uses mypy for static type checking. It appears that pyfakefs does not have type annotations. I'm wondering if there are any plans for this? Also, I would be happy to help contribute!
The text was updated successfully, but these errors were encountered: