-
-
Notifications
You must be signed in to change notification settings - Fork 343
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
Static tools doesn't see class members (at least for trio.Path) #2630
Comments
The magic with
(that being said, I still do not like |
Pathlib will actually be subclassable, as of 3.12. Before then you'd have to set the For statically defining the methods, that could be done for mypy by using |
Hm, might be worth rewriting
I wasn't sure if jedi would check I wrote a test, and other than
* The dunders are a mess and idk how much of it is stuff that's a problem / fixable / hallucinated vs hidden by jedi/dir/inspect.getmembers or similar. So I'd personally lean towards ignoring them for now and adressing that if/when anybody encounters an error in practice. |
I have no idea why |
Well, |
Typecheckers won't like that, FWIW. (Cause subclassing by overriding methods with async methods). We already have https://github.com/python-trio/trio/blob/4f17d2b2693d961f2c12df76a5ce4f80ec3f3178/trio/_path.pyi, maybe it's time to fill that! I don't know about pylint, but mypy, pyright, and (supposedly) jedi will support us if we just add some stubs in there. Maybe. A better solution IMO would be adding stub methods on the class itself (with proper types but that just raise |
ooh thanks, this lead me to https://stackoverflow.com/a/18035950/15544350 which resolved my confusion |
Oh yeah, derp
Oh I'd completely forgotten about the stub file - I was banging my head against the wall the other day because mypy didn't see my updates to
It would be nice not to have to write out the full stub of 50 functions, and be able to do it with some wrapper/decorator that mypy+docgen+jedi accepts. So one can write class Path:
[...]
is_socket = make_async(pathlib.Path.make_async)
with_suffix = make_async(pathlib.Path.with_suffix)
[...] Although actually, at that point it might just be possible to get rid of |
I don't think it'll work for Jedi (davidhalter/jedi#1812), but you can do that wrapper via ParamSpec: from typing import TypeVar, ParamSpec, Awaitable
ArgsT = ParamSpec('ArgsT')
RetT = TypeVar('RetT')
def make_async(func: Callable[ArgsT, RetT) -> Callable[ArgsT, Awaitable[RetT]]: ... |
turns out testing things is a good idea ... this doesn't seem to work?
|
Ahh, mypy is picking up on the self argument not matching. Should be resolvable via def make_async(func: Callable[Concatenate[pathlib.Path, ArgsT], RetT]) -> Callable[Concatenate[trio.Path, ArgsT], Awaitable[RetT]]: ... |
wow now we're getting into real dark typing magic, alas still no go: trio/_path.py:211: error: Argument 1 to "_make_async" has incompatible type "Callable[[], Path]"; expected "Callable[[Path], Path]" [arg-type]
...
trio/_path.py:245: error: Argument 1 to "_make_async" has incompatible type "Callable[[Self, str], Self]"; expected "Callable[[Path, str], Self]" [arg-type]
trio/_path.py:245: note: This is likely because "with_suffix of PurePath" has named arguments: "self". Consider marking them positional-only loads of the latter, just a few of the former |
oh, it also fails with trio/_path.py:23: error: The first argument to Callable must be a list of types, parameter specification, or "..." [valid-type]
trio/_path.py:23: note: See https://mypy.readthedocs.io/en/stable/kinds_of_types.html#callable-types-and-lambdas at the definition 🙃 edit: wait no, stopped getting that error for some reason |
Jedi:
mypy also doesn't see them, which is probably the bigger problem
which means downstream users will get typing errors if they use
trio.Path
in their code.I haven't figured out how to check pylint, but otherwise it should be doable to modify
test_static_tool_sees_all_symbols
to also check class members.For
trio.Path
, there's presumably some reason it's doing lots of magic instead of inheriting fromPath
- so if that's not on the table one would either have to define all the methods, or if that's what_forward_magic
is for it's maybe possible to list them there, or some other constant.The text was updated successfully, but these errors were encountered: