I'm getting a "TypeError: '<' not supported between instances of 'NoneType' and 'str'" when running a python script. Below is the Traceback.
Traceback (most recent call last):
File "c:\python38\lib\runpy.py", line 193, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\python38\lib\runpy.py", line 86, in run_code
exec(code, run_globals)
File "C:\Python38\Scripts\linkrot.exe_main.py", line 7, in
File "c:\python38\lib\site-packages\linkrot\cli.py", line 215, in main
text = get_text_output(pdf, args)
File "c:\python38\lib\site-packages\linkrot\cli.py", line 126, in get_text_output
for k, v in sorted(pdf.get_metadata().items()):
TypeError: '<' not supported between instances of 'NoneType' and 'str'.
Here is the snippet of the code throwing the error. I understand why it is wrong, but I'm unsure how to fix it. Any help would be appreciated.
def get_text_output(pdf, args):
""" Normal output of infos of linkrot instance """
# Metadata
ret = ""
ret += "Document infos:\n"
for k, v in sorted(pdf.get_metadata().items()):
if v:
ret += "- %s = %s\n" % (k, parse_str(v).strip("/"))
# References
ref_cnt = pdf.get_references_count()
ret += "\nReferences: %s\n" % ref_cnt
refs = pdf.get_references_as_dict()
for k in refs:
ret += "- %s: %s\n" % (k.upper(), len(refs[k]))
if args.verbose == 0:
if "pdf" in refs:
ret += "\nPDF References:\n"
for ref in refs["pdf"]:
ret += "- %s\n" % ref
elif ref_cnt:
ret += "\nTip: You can use the '-v' flag to see all references\n"
else:
if ref_cnt:
for reftype in refs:
ret += "\n%s References:\n" % reftype.upper()
for ref in refs[reftype]:
ret += "- %s\n" % ref
return ret.strip()
It appears None
is one of the dictionary keys. Since the sort
method uses comparison to sort the dictionary by keys, it fails because it cannot compare this None
to the other keys that are strings (thus the error message that talks about a comparison operator <
).
Assuming the None
key is there by accident and doesn't contain any useful data, you can solve this by simply removing it from the dictionary before the for loop like this:
metadata = pdf.get_metadata()
metadata.pop(None, None)
for k, v in sorted(metadata.items()):
...