-
Notifications
You must be signed in to change notification settings - Fork 106
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 stubs for typing #232
Add stubs for typing #232
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will be nice to squash to one commit.
Also, as far as I'm concerned this is pretty much done, not WIP :) Unless there is something more you want to add?
Sure. Would be nice to do it via GitHub's UI, but it's disabled here also. As for "WIP": I think it's good to use it via this/your branch for a while before merging. |
Do you mean use it in pytest or something else? If pytest, then there is not much using the stubs beyond running mypy on the code with the stubs installed, and we did do that. Are there any other projects using |
Yes. But I do not think we need to merge it already, and certainly not until others agree that it should be e.g. shipped.
I don't know of any. |
I suspect that with a WIP label it won't get attention by others. I think the stubs are complete, and there is not much to test beyond just using it once in pytest, so I just wonder what more you think is needed in order to get the WIP label down. |
@bluetech agreed. Not really WIP, but waiting for feedback and should not get merged as-is. I've removed the label. |
@blueyed We should add this diff: diff --git a/py/path.pyi b/py/path.pyi
index acbee979..2556a732 100644
--- a/py/path.pyi
+++ b/py/path.pyi
@@ -49,7 +49,7 @@ if sys.version_info >= (3, 6):
else:
class _PathLike(Generic[AnyStr]):
def __fspath__(self) -> AnyStr: ...
-_PathType = Union[bytes, Text, _PathLike[Any]]
+_PathType = Union[bytes, Text, _PathLike[Any], local]
class local:
class ImportMismatchError(ImportError): ... While I thought |
@bluetech ok, please push it here then - I cannot really tell/help in that regard. |
Huh, didn't notice this PR is now based on my branch. I pushed this and another fix. |
@bluetech please |
You might also want to fix the conflict with appveyor.yml, so that CI runs. |
@bluetech re the above: I do not have permission to push to this branch also (allow it via checkbox on the right?). But would still not allow me to push tags probably. |
@blueyed done. Do you think there is anything else blocking this PR? Should we ask anybody for additional review or +1s? |
Not really, apart from that we/you are still added fixes/improvements to it.
Sure.
I'm going to add this branch to the "mypy" tox env, which then can be run on CI already. Having the version (tag) for it was a requirement. |
Okay, pinging some names I recognize from the contributor's list. @nicoddemus @RonnyPfannschmidt @asottile This PR adds type stubs to Would be great to have your +1's, unless you think this is not a good idea. |
the stubs seem fine, though for a library which is "maintenance only" I think this extends a bit beyond that -- that said it does improve pytest's ability to typecheck itself so I'm a little torn 🤔 |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
py/path.pyi
Outdated
def __fspath__(self) -> AnyStr: ... | ||
_PathType = Union[bytes, Text, _PathLike[Any], local] | ||
|
||
class local: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class local: | |
class local(_PathLike[str]): |
This would allow for pathlib.Path(py.path.local("foo"))
then, where mypy currently complains:
Argument 1 to "Path" has incompatible type "local"; expected "Union[str, _PathLike[str]]" [arg-type]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that it should only accept Text in general (#238).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would allow for pathlib.Path(py.path.local("foo")) then, where mypy currently complains:
The situation is odd. If we also do it for the real local
(in the .py
, not just in the .pyi
) then that would remove any doubt from my mind.
For now a workaround for pathlib.Path(py.path.local("foo"))
is pathlib.Path(str(py.path.local("foo")))
. This is also be portable to Python<=3.5.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the workaround is known, but currently mypy complains about something that works (with py36+).
We can leave it out for now if unclear, but should probably make it more strict to not accepts bytes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can leave it out for now if unclear
If we make the real local
derive from os.PathLike
, then it would be good to add it in the stubs too. But if not, then the stub would be sort-of lying. Although os.PathLike
has a hack where isinstance(foo, os.PathLike)
returns true even if foo
just implements __fspath__
, without deriving. So I'm not really sure.
but should probably make it more strict to not accepts bytes?
While it seems bytes
support is broken, I'm sure there's someone someplace that uses it. For example #187 (comment). So again, not sure :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An article on how pathlib (does not) handle this: https://jodal.no/2019/12/10/pathlib-and-paths-with-arbitrary-bytes/
commit cae97e28 (HEAD -> pyi)
Author: Daniel Hahler <git@thequod.de>
Date: Tue Jan 28 22:14:25 2020 +0100
path: __truediv__, __div__, join: returns local
diff --git a/py/path.pyi b/py/path.pyi
index b7879c3e..e803ada2 100644
--- a/py/path.pyi
+++ b/py/path.pyi
@@ -65,8 +65,8 @@ class local:
def __gt__(self, other: object) -> bool: ...
def __add__(self, other: object) -> local: ...
def __cmp__(self, other: object) -> int: ...
- def __div__(self, other: _PathType): ...
- def __truediv__(self, other: _PathType): ...
+ def __div__(self, other: _PathType) -> local: ...
+ def __truediv__(self, other: _PathType) -> local: ...
def __fspath__(self) -> str: ...
@classmethod
@@ -132,7 +132,7 @@ class local:
def isdir(self) -> bool: ...
def isfile(self) -> bool: ...
def islink(self) -> bool: ...
- def join(self, *args: _PathType, abs: int = ...) -> Any: ...
+ def join(self, *args: _PathType, abs: int = ...) -> local: ...
def listdir(
self,
fil: Optional[Union[str, Text, Callable[[local], bool]]] = ..., |
Codecov Report
@@ Coverage Diff @@
## master #232 +/- ##
=======================================
Coverage 82.06% 82.06%
=======================================
Files 55 55
Lines 10161 10161
Branches 1141 1141
=======================================
Hits 8339 8339
Misses 1558 1558
Partials 264 264 |
@blueyed Oops. Applied now. I also went and squashed it. I know you asked not to, but it was pretty terrible! I put the old history here: https://github.com/bluetech/py/tree/pyi-old |
Fixes: > Argument 1 to "Path" has incompatible type "Union[local, Any]"; > expected "Union[str, _PathLike[str]]" [arg-type] Ref: pytest-dev/py#232 (review)
Fixes: > Argument 1 to "Path" has incompatible type "Union[local, Any]"; > expected "Union[str, _PathLike[str]]" [arg-type] Ref: pytest-dev/py#232 (review)
Since this doesn't make any code changes, I think the risk is quite low. But I understand the hesitation. To move this forward, maybe we can submit it to typeshed instead. @blueyed, WDYT? |
@bluetech |
I see 3 options to proceed:
That's why going through typeshed seems like the best course to me for now. But of course I wouldn't submit it without consensus. |
im ok with option 2 in a feature release - this is clearly a help for any downstream that wants to adopt typing |
Added a few improvements to py.xml while adding annotations to _pytest.junitxml. |
... and squashed again. @RonnyPfannschmidt said:
Thanks! So I guess we have +1 from @blueyed, @RonnyPfannschmidt (and me), and a -0 (?) from @asottile. If anyone with permissions feels like merging this (and maybe releasing), that would be great. If there will be any issues reported after releasing this, I'll make sure to handle them (I subscribed to issues on this repo). |
There were a couple of omissions in the py.xml types, I fixed them:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, should we update the CHANGELOG?
Add complete type stubs to the public interface of some of `py`s modules. The modules are those used by `pytest`. The types are marked as partial, so uncovered modules should be unaffected. Co-authored-by: Ran Benita <ran@unusedvar.com>.
OK, I made a few updates:
Finally, pytest master is currently completely clean with this PR. So I think this is ready. |
Awesome! Let's merge and release this then! |
This is meant to be used for pytest, which mainly uses py.path.local, and is very much work in progress.
Continuation of #231