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

Update and provide fixes for mypy pre-commit #12389

Merged
merged 16 commits into from
Nov 7, 2023

Conversation

notatallshaw
Copy link
Member

Update and provide fixes for mypy pre-commit

When running pre-commit locally with Python 3.12 on Pip main I am getting the error:

src/pip/_internal/utils/misc.py:148: error: Unexpected keyword argument "onexc"
for "rmtree"  [call-arg]
            shutil.rmtree(dir, onexc=handler)
            ^
/home/damian/.cache/pre-commit/repozzcb1fld/py_env-python3.12/lib/python3.12/site-packages/mypy/typeshed/stdlib/shutil.pyi:85: note: "rmtree" defined here
Found 1 error in 1 file (checked 293 source files)

I assume this is because the version of MyPy in the pre-commit is quite old. I updated it and then went through all the new errors it produced trying to fix them as seemed most appropriate.

I ended up having to add a type-ignore on this line regardless as although it now recognizes onexc it can not translate the type:

Argument "onexc" to "__call__" of "_RmtreeType" has incompatible type "Callable[[FunctionType, Path, tuple[type[BaseException], BaseException, TracebackType]], Any]"; expected "Callable[[Callable[..., Any], str, Exception], object]"

@@ -941,7 +945,7 @@ def html_index_with_onetime_server(
"""

class InDirectoryServer(http.server.ThreadingHTTPServer):
def finish_request(self, request: Any, client_address: Any) -> None:
def finish_request(self: "Self", request: Any, client_address: Any) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

Why Mypy requires this change I don’t understand

Copy link
Member Author

Choose a reason for hiding this comment

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

Without it the call below it:

self.RequestHandlerClass(
    request,
    client_address,
    self,
    directory=str(html_index_for_packages),  # type: ignore[call-arg]
)

Causes the mypy error:

tests/conftest.py:952: error: Argument 3 has incompatible type
"InDirectoryServer"; expected "Self"  [arg-type]
                    self,
                    ^~~~

I did not investigate why mypy now requires self in this case to be type hinted.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I kind of understand the reason Mypy needs it, but it really shouldn’t need it. It could be a bug.

@pradyunsg pradyunsg merged commit 2a0acb5 into pypa:main Nov 7, 2023
26 checks passed
@pradyunsg
Copy link
Member

Merging this on the basis of the approvals here.

fridex pushed a commit to fridex/pip that referenced this pull request Nov 8, 2023
* Update mypy to 1.6.1

* Fix mypy "Source file found twice under different module names" error

* Ignore type of intialized abstract class in tests

* Use more specific type ignore method-assign

* Type ignore for message.get_all

* Remove unused type ignore

* Add SizedBuffer type for xmlrpc.client.Transport subclass

* Add Self type for RequestHandlerClass in test

* Add type ignore for shutil.rmtree onexc handler

* Quote SizedBuffer

* Add news entry

* Remove no longer correct comment

* Update self import

* Also ignore type onerror=handler

* Update news entry

* Update news entry
@notatallshaw notatallshaw mentioned this pull request Nov 19, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants