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

distutils & setuptools: Relax path related params #11948

Merged
merged 10 commits into from
May 21, 2024

Conversation

Avasam
Copy link
Collaborator

@Avasam Avasam commented May 18, 2024

This started as wanting to resolve this setuptools TODO: https://github.com/pypa/setuptools/blob/main/setuptools/command/editable_wheel.py#L469-L471
so it can start running mypy on Python 3.12: pypa/setuptools#4352 (comment)

But I ended up going all-out. Doing more than just copy_file and its used methods. Especially since I'm not concerned about the implementation changing given that distutils is retired and I'm adding first-party annotations to setuptools, keeping stubs and library in sync.

This comment has been minimized.

This comment has been minimized.

@srittau
Copy link
Collaborator

srittau commented May 18, 2024

This has conflicts now.

This comment has been minimized.


@overload
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need these overloads? Looks like only base_name and root_dir are different, and when I look at the implementation in 3.11, I don't see any interdependency between those.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If root_dir is not None, base_name can be PathLike[str]
https://github.com/python/cpython/blob/v3.11.9/Lib/distutils/archive_util.py#L225-L227

Copy link
Member

Choose a reason for hiding this comment

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

I see. I'm not enthusiastic about adding overloads for cases like this where some types work by accident depending on the path taken through the body of the function.

@@ -80,4 +81,4 @@ class config(Command):
self, header: str, include_dirs: Sequence[str] | None = None, library_dirs: Sequence[str] | None = None, lang: str = "c"
) -> bool: ...

def dump_file(filename: str, head: Any | None = None) -> None: ...
def dump_file(filename: FileDescriptorOrPath, head: Any | None = None) -> None: ...
Copy link
Member

Choose a reason for hiding this comment

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

File descriptors don't work with the log.info('%s', filename) line in the implementation. I suppose we could overload on the value of head but that seems like overkill.

Copy link
Collaborator Author

@Avasam Avasam May 18, 2024

Choose a reason for hiding this comment

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

Doesn't printf-style '%s' % value just coerce to string? https://github.com/python/cpython/blob/v3.11.9/Lib/distutils/log.py#L25

And yeah if FileDescriptorOrPath doesn't fit all code paths for a method, it's overkill to overload it.
Similarly if there's a FileDescriptorOrPath that is technically type-safe but you believe doesn't make sense in context, I don't mind changing it to StrOrBytesPath.

Copy link
Member

Choose a reason for hiding this comment

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

Oh right, I've been writing too much C where this would segfault. I guess we can allow file descriptors here if they would work at runtime, but I'm sort of dubious on how helpful it is. There's a benefit to adding fds to the allowed types in our stubs (someone could be intentionally passing in an fd) but also a cost (someone could accidentally pass in some random int and now they won't see a type error), and I'm not convinced the benefits outweigh the costs.

Then again, I don't really use distutils, so I'm happy to defer to your judgment.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't really use distutils, so I'm happy to defer to your judgment.

I care most about allowing PathLike wherever possible, even if it's an implementation detail. especially for methods used by, or overridden by, setuptools.
FileDescriptorOrPath is candy on top.

but also a cost (someone could accidentally pass in some random int and now they won't see a type error), and I'm not convinced the benefits outweigh the costs.
but I would personally skip some of the overly precise types that seem to be encoding implementation details.

You make a good point. Eh, I'm not attached to it. I won't keep the filedescriptors params

stdlib/distutils/dep_util.pyi Outdated Show resolved Hide resolved

This comment has been minimized.

@Avasam Avasam requested a review from JelleZijlstra May 18, 2024 18:04
Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

I'm fine merging this if you fix the CI issue (unused allowlist entry), but I would personally skip some of the overly precise types that seem to be encoding implementation details.

Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@JelleZijlstra JelleZijlstra merged commit a375953 into python:main May 21, 2024
57 checks passed
@Avasam Avasam deleted the distutils-relax-path-params branch May 21, 2024 06:24
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.

3 participants