Skip to content

Fix pos-only params in os module #11505

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

Merged
merged 3 commits into from
Feb 29, 2024
Merged

Fix pos-only params in os module #11505

merged 3 commits into from
Feb 29, 2024

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Feb 29, 2024

Problem:

/home/runner/work/typeshed/typeshed/stdlib/os/__init__.pyi
  /home/runner/work/typeshed/typeshed/stdlib/os/__init__.pyi:872:33 - error: Position-only parameter not allowed after parameter that is not position-only (reportGeneralTypeIssues)
  /home/runner/work/typeshed/typeshed/stdlib/os/__init__.pyi:873:34 - error: Position-only parameter not allowed after parameter that is not position-only (reportGeneralTypeIssues)

It is the same notation for __arg1: X, *args: X
Link: https://mypy-play.net/?mypy=latest&python=3.12&gist=b35729b668bf88a0d98a78a8d37c5be1

This comment has been minimized.

Comment on lines +872 to +873
def execl(file: StrOrBytesPath, *args: Unpack[tuple[StrOrBytesPath, Unpack[tuple[StrOrBytesPath, ...]]]]) -> NoReturn: ...
def execlp(file: StrOrBytesPath, *args: Unpack[tuple[StrOrBytesPath, Unpack[tuple[StrOrBytesPath, ...]]]]) -> NoReturn: ...
Copy link
Member

Choose a reason for hiding this comment

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

Using PEP646 feels unnecessarily complicated here (and e.g. pytype still doesn't support PEP646 -- it just falls back to Any when it sees Unpack, I believe). Can't we just do this?

Suggested change
def execl(file: StrOrBytesPath, *args: Unpack[tuple[StrOrBytesPath, Unpack[tuple[StrOrBytesPath, ...]]]]) -> NoReturn: ...
def execlp(file: StrOrBytesPath, *args: Unpack[tuple[StrOrBytesPath, Unpack[tuple[StrOrBytesPath, ...]]]]) -> NoReturn: ...
def execl(file: StrOrBytesPath, arg0: StrOrBytesPath, /, *args: StrOrBytesPath) -> NoReturn: ...
def execlp(file: StrOrBytesPath, arg0: StrOrBytesPath, /, *args: StrOrBytesPath) -> NoReturn: ...

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, looks easier!

@AlexWaygood
Copy link
Member

Oh interesting. Stubtest is correct; you really can pass file as a keyword argument:

>>> import os
>>> os.execl(file='foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<frozen os>", line 548, in execl
ValueError: execv() arg 2 must not be empty

@sobolevn
Copy link
Member Author

Hm, this way we have a stubtest problem:

error: os.execl is inconsistent, stub argument "file" should be positional or keyword (remove leading double underscore)
Stub: in file stdlib/os/__init__.pyi:872
def (Union[builtins.str, builtins.bytes, os.PathLike[builtins.str], os.PathLike[builtins.bytes]], Union[builtins.str, builtins.bytes, os.PathLike[builtins.str], os.PathLike[builtins.bytes]], *args: Union[builtins.str, builtins.bytes, os.PathLike[builtins.str], os.PathLike[builtins.bytes]]) -> Never
Runtime: in file /opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/os.py:538
def (file, *args)

error: os.execlp is inconsistent, stub argument "file" should be positional or keyword (remove leading double underscore)
Stub: in file stdlib/os/__init__.pyi:873
def (Union[builtins.str, builtins.bytes, os.PathLike[builtins.str], os.PathLike[builtins.bytes]], Union[builtins.str, builtins.bytes, os.PathLike[builtins.str], os.PathLike[builtins.bytes]], *args: Union[builtins.str, builtins.bytes, os.PathLike[builtins.str], os.PathLike[builtins.bytes]]) -> Never
Runtime: in file /opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/os.py:553
def (file, *args)

This comment has been minimized.

@AlexWaygood
Copy link
Member

I guess your PEP646 way is the best way, in that case :)

Copy link
Contributor

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

@AlexWaygood AlexWaygood merged commit 217166b into main Feb 29, 2024
@AlexWaygood AlexWaygood deleted the pos-only-os branch February 29, 2024 09:01
danieleades pushed a commit to danieleades/typeshed that referenced this pull request Mar 1, 2024
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.

2 participants