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

Resolve all "mypy: strict-optional=False" overrides #10439

Closed
wants to merge 1 commit into from
Closed

Resolve all "mypy: strict-optional=False" overrides #10439

wants to merge 1 commit into from

Conversation

jdufresne
Copy link
Contributor

No description provided.

@@ -87,6 +83,7 @@ def distutils_scheme(
prefix = i.install_userbase # type: ignore
else:
prefix = i.prefix
assert prefix is not None
Copy link
Member

Choose a reason for hiding this comment

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

Probably better to annotate prefix as str 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.

Right now the argument is defined as prefix: str = None. Are you suggesting to make the default an empty string or to make the argument required?

Comment on lines 15 to 16
site_packages: typing.Optional[str] = sysconfig.get_path("purelib")
_site_packages = sysconfig.get_path("purelib")
assert _site_packages is not None
site_packages = _site_packages
Copy link
Member

Choose a reason for hiding this comment

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

This can theoratically be None, and pip should not crash on import-time IMO. I have some refactorings for this included in #10511.

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 see. I've restored mypy: strict-optional=False in misc.py. It can be revisited once that other work is finished.

Comment on lines 177 to 173
location: str,
location: Optional[str],
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this should be None in any cases; the assertion should probably be done at the caller site.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, according to test tests/functional/test_build_env.py::test_build_env_allow_only_one_install this value can be None when:

link.url not in downloaded
link.is_vcs=False
link.is_existing_dir()=False
link.is_file=True
link.is_wheel=True

During this test, req.source_dir is None.

Traceback (most recent call last):
  File "/tmp/pip-standalone-pip-z_myip83/__env_pip__.zip/pip/_internal/cli/base_command.py", line 179, in exc_logging_wrapper
    status = run_func(*args)
  File "/tmp/pip-standalone-pip-z_myip83/__env_pip__.zip/pip/_internal/cli/req_command.py", line 203, in wrapper
    return func(self, options, args)
  File "/tmp/pip-standalone-pip-z_myip83/__env_pip__.zip/pip/_internal/commands/install.py", line 340, in run
    requirement_set = resolver.resolve(
  File "/tmp/pip-standalone-pip-z_myip83/__env_pip__.zip/pip/_internal/resolution/resolvelib/resolver.py", line 94, in resolve
    result = self._result = resolver.resolve(
  File "/tmp/pip-standalone-pip-z_myip83/__env_pip__.zip/pip/_vendor/resolvelib/resolvers.py", line 472, in resolve
    state = resolution.resolve(requirements, max_rounds=max_rounds)
  File "/tmp/pip-standalone-pip-z_myip83/__env_pip__.zip/pip/_vendor/resolvelib/resolvers.py", line 341, in resolve
    self._add_to_criteria(self.state.criteria, r, parent=None)
  File "/tmp/pip-standalone-pip-z_myip83/__env_pip__.zip/pip/_vendor/resolvelib/resolvers.py", line 172, in _add_to_criteria
    if not criterion.candidates:
  File "/tmp/pip-standalone-pip-z_myip83/__env_pip__.zip/pip/_vendor/resolvelib/structs.py", line 151, in __bool__
    return bool(self._sequence)
  File "/tmp/pip-standalone-pip-z_myip83/__env_pip__.zip/pip/_internal/resolution/resolvelib/found_candidates.py", line 155, in __bool__
    return any(self)
  File "/tmp/pip-standalone-pip-z_myip83/__env_pip__.zip/pip/_internal/resolution/resolvelib/found_candidates.py", line 143, in <genexpr>
    return (c for c in iterator if id(c) not in self._incompatible_ids)
  File "/tmp/pip-standalone-pip-z_myip83/__env_pip__.zip/pip/_internal/resolution/resolvelib/found_candidates.py", line 47, in _iter_built
    candidate = func()
  File "/tmp/pip-standalone-pip-z_myip83/__env_pip__.zip/pip/_internal/resolution/resolvelib/factory.py", line 201, in _make_candidate_from_link
    self._link_candidate_cache[link] = LinkCandidate(
  File "/tmp/pip-standalone-pip-z_myip83/__env_pip__.zip/pip/_internal/resolution/resolvelib/candidates.py", line 281, in __init__
    super().__init__(
  File "/tmp/pip-standalone-pip-z_myip83/__env_pip__.zip/pip/_internal/resolution/resolvelib/candidates.py", line 156, in __init__
    self.dist = self._prepare()
  File "/tmp/pip-standalone-pip-z_myip83/__env_pip__.zip/pip/_internal/resolution/resolvelib/candidates.py", line 225, in _prepare
    dist = self._prepare_distribution()
  File "/tmp/pip-standalone-pip-z_myip83/__env_pip__.zip/pip/_internal/resolution/resolvelib/candidates.py", line 292, in _prepare_distribution
    return preparer.prepare_linked_requirement(self._ireq, parallel_builds=True)
  File "/tmp/pip-standalone-pip-z_myip83/__env_pip__.zip/pip/_internal/operations/prepare.py", line 492, in prepare_linked_requirement
    return self._prepare_linked_requirement(req, parallel_builds)
  File "/tmp/pip-standalone-pip-z_myip83/__env_pip__.zip/pip/_internal/operations/prepare.py", line 539, in _prepare_linked_requirement
    assert req.source_dir is not None
AssertionError

Do you think this is a non-representative test or that location can in fact be None?

Comment on lines 300 to 301
if link and link.hash:
assert link.hash_name is not None
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if link and link.hash:
assert link.hash_name is not None
if link and link.hash_name and link.hash:

Comment on lines 311 to 313
comes_from: Optional[str]
if isinstance(self.comes_from, str):
comes_from = self.comes_from
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
comes_from: Optional[str]
if isinstance(self.comes_from, str):
comes_from = self.comes_from
if isinstance(self.comes_from, str):
comes_from: Optional[str] = self.comes_from

Comment on lines 877 to 878
assert exc.__cause__ is not None
raise exc.__cause__
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert exc.__cause__ is not None
raise exc.__cause__
if exc.__cause__ is not None:
raise exc.__cause__
raise

Maybe?

Copy link
Member

Choose a reason for hiding this comment

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

Or maybe we should add an attribute on LegacyInstallFailure instead of depending on __cause__.

Comment on lines 475 to 476
assert parsed.hostname is not None
return parsed.hostname, parsed.port
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert parsed.hostname is not None
return parsed.hostname, parsed.port
return parsed.hostname or "", parsed.port

@jdufresne jdufresne marked this pull request as draft September 30, 2021 17:57
Comment on lines 45 to 49
user_site: typing.Optional[str] = site.getusersitepackages()
user_site = site.getusersitepackages()
except AttributeError:
user_site = site.USER_SITE
_user_site = site.USER_SITE
assert _user_site is not None
user_site = _user_site
Copy link
Member

Choose a reason for hiding this comment

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

Same here, this value can be None if the user does not have the user site enabled.

Comment on lines 504 to 511
if self.download_dir is not None:
assert req.link is not None
if req.link.is_wheel:
hashes = self._get_linked_req_hashes(req)
file_path = _check_download_dir(req.link, self.download_dir, hashes)
if file_path is not None:
self._downloaded[req.link.url] = file_path
req.needs_more_preparation = False
Copy link
Member

Choose a reason for hiding this comment

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

This can be if self.download_dir is not None and req.link is not None and req.link.is_wheel: which is semantically slightly different but do the same thing functionally.

@github-actions github-actions bot added the needs rebase or merge PR has conflicts with current master label Oct 2, 2021
@pypa-bot pypa-bot removed the needs rebase or merge PR has conflicts with current master label Oct 2, 2021
Overrides remain in src/pip/_internal/locations/base.py.
Comment on lines 21 to +23
class LegacyInstallFailure(Exception):
pass
def __init__(self, cause: Exception):
self.cause = cause
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 we need to call super().__init__(cause) as well.

@jdufresne jdufresne closed this Oct 17, 2021
@jdufresne jdufresne deleted the optional-strict branch October 17, 2021 03:17
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 1, 2021
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