proycon / codemetapy

A Python package for generating and working with codemeta
https://codemeta.github.io/
GNU General Public License v3.0
24 stars 5 forks source link

Fails if Python package has no requirements: TypeError: 'NoneType' object is not iterable #16

Closed nealmcb closed 2 years ago

nealmcb commented 2 years ago

I'm trying to run codemetapy on my rlacalc Python package, and ran into two problems. Here they are, with workarounds for both of them.

Based on this from the README:

If you are in the current working directory of any python project, i.e. there is a setup.py, then you can simply call codemetapy without arguments to output codemeta for the project.

I just tried running it in the root directory, which didn't work:

Exception: No input files specified (use - for stdin)

Since modern Python development often doesn't involve the dangerous process of running setup.py, I suggest adding the ability to parse a pyproject.toml file, and in the meantime, changing the README to say something more like "If you are in the current working directory of a python project with a setup.py, ..."

So I switched to adding a package name, and that also failed:

$ codemetapy rlacalc
Passed 1 files/sources but specified 0 input types! Automatically guessing types...
Detected input types: [('rlacalc', 'python')]
Downloading context from https://raw.githubusercontent.com/codemeta/codemeta/2.0/codemeta.jsonld
Downloading context from https://raw.githubusercontent.com/schemaorg/schemaorg/main/data/releases/13.0/schemaorgcontext.jsonld
Downloading context from https://w3id.org/software-types
Downloading context from https://raw.githubusercontent.com/proycon/repostatus.org/ontology/badges/latest/ontology.jsonld
URI automatically generated, may be overriden later: /rlacalc
Processing source #1 of 1
Obtaining python package metadata for: rlacalc
Found metadata in /home/neal/.local/lib/python3.8/site-packages/rlacalc-0.3.0.dist-info
WARNING: No translation for distutils key Metadata-Version
WARNING: No translation for distutils key Requires-Python
Traceback (most recent call last):
  File "/home/neal/.local/bin/codemetapy", line 8, in <module>
    sys.exit(main())
  File "/home/neal/.local/lib/python3.8/site-packages/codemeta/codemeta.py", line 128, in main
    output = build(**args.__dict__)
  File "/home/neal/.local/lib/python3.8/site-packages/codemeta/codemeta.py", line 290, in build
    prefuri = codemeta.parsers.python.parse_python(g, res, source, crosswalk, args)
  File "/home/neal/.local/lib/python3.8/site-packages/codemeta/parsers/python.py", line 121, in parse_python
    for value in pkg.requires:
TypeError: 'NoneType' object is not iterable

That seems to be because I don't have a requirements file, nor any requirements beyond Python....

It works to guard the for loop in line 121 like this:

if pkg.requires is not None:
    for value in pkg.requires:
....

But I also wonder if importlib shouldn't make that an empty array instead of None. So I'll leave the fix up to you.

proycon commented 2 years ago

Thanks for the feedback! I have implemented your solution now so at least the system won't stumble over having no dependencies.

I agree that things should be made to work with only a pyproject.toml instead of a setup.py, especially as that's the recommended way for the future. I'm currently investigating if I can get importlib.metadata to get to that info, without necessarily installing the entire package and all dependencies. I'd rather not reinvent the wheel and parse the toml manually.

proycon commented 2 years ago

I released v2.0.1 with the fix, closing this issue, the support for pyproject.toml is tracked separately in #17 now.