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

AttributeError: 'NoneType' object has no attribute 'is_hash_allowed' #6772

Closed
ptynecki opened this issue Jul 23, 2019 · 18 comments · Fixed by #6774
Closed

AttributeError: 'NoneType' object has no attribute 'is_hash_allowed' #6772

ptynecki opened this issue Jul 23, 2019 · 18 comments · Fixed by #6774
Labels
C: list/show S: auto-locked type: bug
Milestone

Comments

@ptynecki
Copy link

@ptynecki ptynecki commented Jul 23, 2019

Environment

  • pip version: 19.2
  • Python version: 3.6.8
  • OS: Mac OSX (Darwin Kernel Version 18.6.0)

Description
I made env update in my project including pip as well. After that I wanted to check outdated packages with command:

pip list --outdated --format=columns

After that exception was raised.

Expected behavior
I expected list of packages or empty list.

How to Reproduce

  1. Get the newest version of package from PyPI.
  2. Then run pip list --outdated --format=columns
  3. An error occurs.

Output

(env) project (develop) $ pip list --outdated --format=columns
ERROR: Exception:
Traceback (most recent call last):
  File "/project/env/lib/python3.6/site-packages/pip/_internal/cli/base_command.py", line 188, in main
    status = self.run(options, args)
  File "/project/env/lib/python3.6/site-packages/pip/_internal/commands/list.py", line 156, in run
    packages = self.get_outdated(packages, options)
  File "/project/env/lib/python3.6/site-packages/pip/_internal/commands/list.py", line 164, in get_outdated
    dist for dist in self.iter_packages_latest_infos(packages, options)
  File "/project/env/lib/python3.6/site-packages/pip/_internal/commands/list.py", line 164, in <listcomp>
    dist for dist in self.iter_packages_latest_infos(packages, options)
  File "/project/env/lib/python3.6/site-packages/pip/_internal/commands/list.py", line 195, in iter_packages_latest_infos
    best_candidate = evaluator.get_best_candidate(all_candidates)
  File "/project/env/lib/python3.6/site-packages/pip/_internal/index.py", line 729, in get_best_candidate
    best_candidate = max(candidates, key=self._sort_key)
  File "/project/env/lib/python3.6/site-packages/pip/_internal/index.py", line 710, in _sort_key
    has_allowed_hash = int(link.is_hash_allowed(self._hashes))
  File "/project/env/lib/python3.6/site-packages/pip/_internal/models/link.py", line 213, in is_hash_allowed
    return hashes.is_hash_allowed(self.hash_name, hex_digest=self.hash)
AttributeError: 'NoneType' object has no attribute 'is_hash_allowed'
@triage-new-issues triage-new-issues bot added the S: needs triage label Jul 23, 2019
@pradyunsg pradyunsg added the type: bug label Jul 23, 2019
@triage-new-issues triage-new-issues bot removed the S: needs triage label Jul 23, 2019
@pradyunsg pradyunsg added the C: list/show label Jul 23, 2019
@pradyunsg pradyunsg added this to the 19.2 milestone Jul 23, 2019
@pradyunsg
Copy link
Member

@pradyunsg pradyunsg commented Jul 23, 2019

@ptynecki Thanks for filing this issue!

/cc @cjerdonek since he's worked on this part of the codebase and is more familiar with it than me.

@MATodd
Copy link

@MATodd MATodd commented Jul 23, 2019

Same issue on Windows 10, Python 3.7.3

@jj1118
Copy link

@jj1118 jj1118 commented Jul 23, 2019

Same issue on Ubuntu 18.04.2 LTS, Python 3.6.8

@matthieumota
Copy link

@matthieumota matthieumota commented Jul 23, 2019

Same issue on macOS 18.6.0 and Python 3.7.4

@bdjurkan
Copy link

@bdjurkan bdjurkan commented Jul 23, 2019

Same issue on Windows 10, Python 3.7.4

@rickhg12hs
Copy link

@rickhg12hs rickhg12hs commented Jul 23, 2019

Same issue on Fedora 29, Python 3.7.3.

For now, as a workaround, ...

python3 -m pip install -UI --user  'pip<19.2'

reinstalled pip version 19.1.1. Although, the complete command output is curious.

$ python3 -m pip install -UI --user  'pip<19.2'
Collecting pip<19.2
  Using cached https://files.pythonhosted.org/packages/5c/e0/be401c003291b56efc55aeba6a80ab790d3d4cece2778288d65323009420/pip-19.1.1-py2.py3-none-any.whl
Installing collected packages: pip
Successfully installed pip-19.2

Why does it report Successfully installed pip-19.2?

@pradyunsg
Copy link
Member

@pradyunsg pradyunsg commented Jul 23, 2019

Thanks for the confirmation of this occurring on multiple OSes!

Folks, if you're facing the same issue, please don't post additional comments. Please use GitHub reactions to upvote the first post and subscribe to the issue.

That way the maintainers would be able to have a discussion on how to resolve this in this issue -- additional "me too" comments won't help anyone.

@cjerdonek
Copy link
Member

@cjerdonek cjerdonek commented Jul 23, 2019

Hmm, I guess the type checker failed us here. The type annotation says hashes must be non-None, but it is None in the reported cases:

def is_hash_allowed(self, hashes):
# type: (Hashes) -> bool
"""
Return True if the link has a hash and it is allowed.
"""
if not self.has_hash:
return False
# Assert non-None so mypy knows self.hash_name and self.hash are str.

@cjerdonek
Copy link
Member

@cjerdonek cjerdonek commented Jul 23, 2019

After a quick look, I think a good fix for now may be to update Link.is_hash_allowed(hashes) to return False if hashes is None (and bring the annotation into alignment by updating the parameter to Optional[Hashes]). That's probably simpler and more sure-fire at this point than trying to update things in possibly multiple places to ensure that the hashes argument is always non-None, especially since we can't seem to lean on the type checker to confirm with 100% certainty.

@pradyunsg
Copy link
Member

@pradyunsg pradyunsg commented Jul 23, 2019

I guess the type checker failed us here.

Ahhh. Yep -- strict_optional is False for pip._internal.index and this is exactly that failure.

we can't seem to lean on the type checker to confirm with 100% certainty.

Yea, we can't completely rely on it, as long as we have these flags for incremental adoption.

@cjerdonek
Copy link
Member

@cjerdonek cjerdonek commented Jul 23, 2019

Ahhh. Yep -- strict_optional is False for pip._internal.index and this is exactly that failure.

And even though models/link.py where the function is defined does have strict_optional enabled.

@cjerdonek
Copy link
Member

@cjerdonek cjerdonek commented Jul 23, 2019

Okay, I posted PR #6774 for this.

@cjerdonek
Copy link
Member

@cjerdonek cjerdonek commented Jul 23, 2019

This issue tells me that a good step after this (for 19.3 -- not the patch release) will be to get strict_optional turned on for index.py..

@pradyunsg
Copy link
Member

@pradyunsg pradyunsg commented Jul 23, 2019

And even though models/link.py where the function is defined does have strict_optional enabled.

Yea - mypy does checks at the call site, using the rules at the call site.


Your suggested fix and PR look good to me. If we don't see any other issue come up, I can do the bugfix release in the evening today -- ~3-5 hours from now.

@cjerdonek
Copy link
Member

@cjerdonek cjerdonek commented Jul 23, 2019

If we don't see any other issue come up, I can do the bugfix release in the evening today -- ~3-5 hours from now.

That would be great. Thank you so much for doing that.

@slafs
Copy link

@slafs slafs commented Jul 23, 2019

Hello. FWIW I may have found a bug in 19.2 that's unrelated to this one. I'm trying to figure it out still and will open a new issue afterwards.

EDIT:
Ah, someone already opened one #6775

@pradyunsg
Copy link
Member

@pradyunsg pradyunsg commented Jul 23, 2019

The fix is in master. I'll do the release in a bit -- getting a bit of rest after a long day today.

@pradyunsg
Copy link
Member

@pradyunsg pradyunsg commented Jul 23, 2019

The release with this bug-fix has been uploaded. Thanks everyone (and especially @cjerdonek)! :)

@pradyunsg pradyunsg unpinned this issue Jul 24, 2019
@lock lock bot added the S: auto-locked label Aug 22, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Aug 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
C: list/show S: auto-locked type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants