Skip to content
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

Compileall rx argument fix #5172

Merged
merged 10 commits into from
Apr 11, 2021
Merged

Compileall rx argument fix #5172

merged 10 commits into from
Apr 11, 2021

Conversation

hatal175
Copy link
Contributor

@hatal175 hatal175 commented Apr 2, 2021

Fixing #3480.

I've added this to all versions since the code seems to use only the search method.
Now, I didn't really want to add SupportsSearch to _typeshed but for some reason, pytype really didn't like it when I put it in the same file.

See here: https://github.com/hatal175/typeshed/runs/2249491861?check_suite_focus=true

I suspect it might have something to with google/pytype#296 or that Protocol isn't subscriptable in pytype stubs but I can't be sure.
If you have other suggestions, I'll be happy to use them.

@rchen152
Copy link
Collaborator

rchen152 commented Apr 2, 2021

I get the pytype error you saw if I import Protocol from typing_extensions. That appears to be due to an oversight in the pytype stub parsing code. (We should also be checking for "typing_extensions.Protocol" there.) This'll be fixed in next week's pytype release.

In the meantime, try either writing (Protocol, Generic[_T_contra]) in the definition of SupportsSearch or importing Protocol from typing; either one of those should fix the error.

@hatal175
Copy link
Contributor Author

hatal175 commented Apr 2, 2021

Thanks Rebecca.

rchen152 added a commit to google/pytype that referenced this pull request Apr 2, 2021
Inheriting from Protocol[T] is a shorthand for inheriting from both Protocol
and Generic[T]. We were handling this case properly for typing.Protocol but not
for typing_extensions.Protocol.

Context: python/typeshed#5172
PiperOrigin-RevId: 366414389
if sys.version_info >= (3, 9):
def compile_dir(
dir: AnyPath,
maxlevels: Optional[int] = ...,
ddir: Optional[AnyPath] = ...,
force: bool = ...,
rx: Optional[Pattern[Any]] = ...,
rx: Optional[_SupportsSearch[AnyStr]] = ...,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AnyStr doesn't make sense to me here; how is the typevar going to get constrained? I'm guessing it depends on whether dir is a bytes or a str path, so perhaps that argument should also have a TypeVar in it.

(Note that AnyPath and AnyStr are very different things: the first is a union and the second a TypeVar.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so compile_dir runs compile_file on every file it can using listdir - which means file will be bytes or str, so rx there can accept str and bytes. which is why I used AnyStr, a type that can be either or.

compile_file however gets AnyPath as file name and can pass that along to rx as is if quiet is 2. Meaning that the rx in compile_file might need to handle the same type as fullname. Which means I might need to fix that? Not sure what to do there.

Either way, I'm not following you. What did you have in mind?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the place I started with is that having a single TypeVar in a signature tends to be a mistake, because the point of a typevar is to use the same type in multiple places. See the discussion in https://mail.python.org/archives/list/typing-sig@python.org/message/NRFNHGXHXPGBR6FP3TIOZZ6VS4XJZX6K/ for some context.

But I just tried it out and at least in 3.7, we don't need all these Unions: compile_dir actually only works on str paths, not bytes paths. In compile_file, it calls importlib.util.cache_from_source, and that only accepts str, not bytes. So instead, let's just change all the parameters dealing with paths to accept only strs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed things to StrPath since it does work with PathLike[str].

@JelleZijlstra
Copy link
Member

Thanks @hatal175!

@hauntsaninja would you mind double-checking me about bytes paths not working at all with compileall? I see we discussed this in #3956 before, and the documentation actually claims bytes paths are supported.

@hatal175
Copy link
Contributor Author

hatal175 commented Apr 11, 2021 via email

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, yeah, the compileall documentation is full of falsehood unfortunately. fullname has to be StrPath because of importlib, ddir has to be StrPath because os.path.join(ddir, os.path.basename(os.fspath(fullname))) (or None), similar for prependdir.

However, limit_sl_dest can still be bytes, and is documented as that being the case, so we should change that one back to AnyPath.

Separately, it looks like compile_dir's dir can be None under some expressible via overload circumstances.

@hatal175
Copy link
Contributor Author

hatal175 commented Apr 11, 2021

Could you give examples of both?
This fails:
compile_file("c:\\downloads\\xxx_link.py", limit_sl_dest=b"C:\\Downloads")
And this fails
compile_dir(None)

And I don't quite see what code path will make them work.

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wrong on both counts and to be honest I don't know what I thought I saw last night. Sorry about that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants