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

Enable mypy's strict equality checks #12209

Merged
merged 6 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
11 changes: 7 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ per-file-ignores =

[mypy]
mypy_path = $MYPY_CONFIG_FILE_DIR/src

strict = True
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 change to setting strict=True and opting out of specific checks.

This effectively enables strict_equality = True and a couple other options that have less of a bearing on the pip codebase. See also https://mypy.readthedocs.io/en/stable/existing_code.html#introduce-stricter-options


no_implicit_reexport = False
allow_subclassing_any = True
allow_untyped_calls = True
warn_return_any = False
ignore_missing_imports = True
disallow_untyped_defs = True
disallow_any_generics = True
warn_unused_ignores = True
no_implicit_optional = True

[mypy-pip._internal.utils._jaraco_text]
ignore_errors = True
Expand Down
4 changes: 2 additions & 2 deletions src/pip/_internal/utils/temp_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import traceback
from contextlib import ExitStack, contextmanager
from pathlib import Path
from types import FunctionType
from typing import (
Any,
Callable,
Dict,
Generator,
List,
Expand Down Expand Up @@ -187,7 +187,7 @@ def cleanup(self) -> None:
errors: List[BaseException] = []

def onerror(
func: FunctionType,
func: Callable[..., Any],
path: Path,
exc_val: BaseException,
) -> None:
Expand Down
3 changes: 1 addition & 2 deletions tests/functional/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,7 @@ def test_outdated_formats(script: PipTestEnvironment, data: TestData) -> None:
"--outdated",
"--format=json",
)
data = json.loads(result.stdout)
assert data == [
assert json.loads(result.stdout) == [
{
"name": "simple",
"version": "1.0",
Expand Down
4 changes: 3 additions & 1 deletion tests/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
# Literal was introduced in Python 3.8.
from typing import Literal

ResolverVariant = Literal["resolvelib", "legacy"]
ResolverVariant = Literal[
"resolvelib", "legacy", "2020-resolver", "legacy-resolver"
]
Comment on lines +49 to +51
Copy link
Member

Choose a reason for hiding this comment

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

I think it'd be better to modify the code paths to enforce the "resolvelib" and "legacy" values consistently instead.

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 made this change locally, but I'm a little less sure of it. Maybe better to do in a separate PR?

Copy link
Member

Choose a reason for hiding this comment

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

Cool! As long as you don't forget to file it. 🙃

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a deal

else:
ResolverVariant = str

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_network_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ def get_credential(self, system: str, username: str) -> Optional[Credential]:
),
)
def test_keyring_get_credential(
monkeypatch: pytest.MonkeyPatch, url: str, expect: str
monkeypatch: pytest.MonkeyPatch, url: str, expect: Tuple[str, str]
) -> None:
monkeypatch.setitem(sys.modules, "keyring", KeyringModuleV2())
auth = MultiDomainBasicAuth(
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from contextlib import contextmanager
from optparse import Values
from tempfile import NamedTemporaryFile
from typing import Any, Dict, Iterator, List, Tuple, Union, cast
from typing import Any, Dict, Iterator, List, Tuple, Type, Union, cast

import pytest

Expand Down Expand Up @@ -605,7 +605,7 @@ def test_config_file_options(
self,
monkeypatch: pytest.MonkeyPatch,
args: List[str],
expect: Union[None, str, PipError],
expect: Union[None, str, Type[PipError]],
) -> None:
cmd = cast(ConfigurationCommand, create_command("config"))
# Replace a handler with a no-op to avoid side effects
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/test_target_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,17 @@ def test_get_sorted_tags(
py_version_info: Optional[Tuple[int, ...]],
expected_version: Optional[str],
) -> None:
mock_get_supported.return_value = ["tag-1", "tag-2"]
dummy_tags = [Tag("py4", "none", "any"), Tag("py5", "none", "any")]
mock_get_supported.return_value = dummy_tags

target_python = TargetPython(py_version_info=py_version_info)
actual = target_python.get_sorted_tags()
assert actual == ["tag-1", "tag-2"]
assert actual == dummy_tags

actual = mock_get_supported.call_args[1]["version"]
assert actual == expected_version
assert mock_get_supported.call_args[1]["version"] == expected_version

# Check that the value was cached.
assert target_python._valid_tags == ["tag-1", "tag-2"]
assert target_python._valid_tags == dummy_tags

def test_get_unsorted_tags__uses_cached_value(self) -> None:
"""
Expand Down
Loading