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

use PEP 420: implicit namespace package #990

Merged
merged 10 commits into from Jul 14, 2022
Merged

Conversation

emlys
Copy link
Member

@emlys emlys commented May 17, 2022

Description

Fixes #967

  • Removed src/natcap/__init__.py to make natcap an implicit namespace package
  • Removed the deprecated namespace_packages parameter to setup. Since we already list out all packages, I don't think we need to change anything else: https://setuptools.pypa.io/en/latest/userguide/package_discovery.html
  • Update doc/api-docs/conf.py to work with the implicit namespace package, and some minor refactoring:
    • Update the text at the top of the model entrypoints page
    • Build the list of model entrypoints from the installed location of natcap.invest, not the build/lib* location
    • Name the model entrypoints doc sections after the model title, not the first line of the execute docstring

Checklist

  • Updated HISTORY.rst (if these changes are user-facing)
  • Updated the user's guide (if needed)
  • Tested the affected models' UIs (if relevant)

module = importlib.import_module(name)
except Exception as ex:
print(ex)
continue
Copy link
Member Author

Choose a reason for hiding this comment

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

Removed this because I think we want to raise an error if a module doesn't import.

Copy link
Member

Choose a reason for hiding this comment

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

I agree, better to fail loudly.

@emlys
Copy link
Member Author

emlys commented May 24, 2022

It turns out that this has some implications for our package data (currently just the placeholder directories for translation catalogs). See discussion: pypa/setuptools#3323

  • PEP 420 is standard and the python interpreter looks for namespace packages by default
  • By design, any directory within a package is also a package, even if it's empty or contains no python files.
  • Therefore, any "data" directories included in a package are really packages
  • natcap.invest.internationalization, natcap.invest.internationalization.locales, and natcap.invest.internationalization.locales.en are packages. The interpreter already finds them:
>>> import natcap.invest.internationalization.locales.en
>>> natcap.invest.internationalization.locales.en.__name__
'natcap.invest.internationalization.locales.en'
  • Therefore, we should stop trying to include them as "data" (with the package_data kwarg) and instead let them be packages.

I'll deal with this separately in #644.

@@ -77,7 +69,7 @@
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None
language = 'en'
Copy link
Member Author

Choose a reason for hiding this comment

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

This was causing an error in the ReadTheDocs build, probably due to a sphinx update.

@emlys emlys marked this pull request as ready for review July 12, 2022 23:40
@emlys emlys requested a review from phargogh July 12, 2022 23:40
@emlys emlys self-assigned this Jul 12, 2022
@emlys
Copy link
Member Author

emlys commented Jul 12, 2022

@phargogh this is finally ready! The RTD build issues fixed themselves in time.

Copy link
Member

@phargogh phargogh left a comment

Choose a reason for hiding this comment

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

Very exciting! I just had a few very small comments and suggestions, but overall this is looking good!

Comment on lines 31 to 32
INVEST_ROOT_DIR = os.path.join(DOCS_SOURCE_DIR, '..', '..')
INVEST_BUILD_DIR = os.path.join(INVEST_ROOT_DIR, 'build')
Copy link
Member

Choose a reason for hiding this comment

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

Are these used anywhere else now that we're properly defining INVEST_LIB_DIR? Might they be able to be removed?

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right, they can be removed

doc/api-docs/conf.py Outdated Show resolved Hide resolved
module = importlib.import_module(name)
except Exception as ex:
print(ex)
continue
Copy link
Member

Choose a reason for hiding this comment

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

I agree, better to fail loudly.

Comment on lines 232 to 235
'_core', # anything ending in '_core'
'recmodel_server',
'recmodel_workspace_fetcher'
'recmodel_workspace_fetcher',
'natcap.invest.ui'
Copy link
Member

Choose a reason for hiding this comment

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

Based on the RTD-rendered docs (https://invest--990.org.readthedocs.build/en/990/api.html for this PR) it looks like maybe this exclusion isn't working. Maybe it never worked? In any case, it would probably be nice to exclude packages, or else just have everything in there since it is in the source code.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this is working as expected - it's excluding those from the model entrypoints file models.rst. But they're still in the API docs.
The list of excluded modules was unnecessary, though. I refactored so that any module with an ARGS_SPEC is included in models.rst.

Comment on lines 250 to 252
# all modules with an execute function should have an ARGS_SPEC
model_title = module.ARGS_SPEC['model_name']
all_modules[model_title] = name
Copy link
Member

Choose a reason for hiding this comment

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

This is so much better than our prior reliance on module-level docstrings!

namespace_packages=['natcap'],
packages=[
'natcap',
'natcap.invest',
'natcap.invest.coastal_blue_carbon',
'natcap.invest.delineateit',
'natcap.invest.ui',
'natcap.invest.ndr',
'natcap.invest.sdr',
'natcap.invest.recreation',
'natcap.invest.scenic_quality',
'natcap.invest.seasonal_water_yield',
],
packages=find_namespace_packages('src'),
Copy link
Member

Choose a reason for hiding this comment

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

Great! It was not immediately clear to me from this function name that normal packages within natcap.invest would also be captured, but that does seem to be the behavior.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, it's confusing! pypa/setuptools#3427

Comment on lines 15 to 25
from datetime import datetime
import importlib
import itertools
import os
import pkgutil
import subprocess
import sys

from datetime import datetime
from sphinx.ext import apidoc
from unittest.mock import MagicMock

import natcap.invest
from sphinx.ext import apidoc
Copy link
Member

Choose a reason for hiding this comment

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

Could you just check these imports and make sure that we aren't importing anything we no longer need?

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right, a couple of them aren't needed any more

emlys and others added 2 commits July 14, 2022 10:01
Co-authored-by: James Douglass <jamesdouglassusa@gmail.com>
@emlys emlys requested a review from phargogh July 14, 2022 17:18
@emlys
Copy link
Member Author

emlys commented Jul 14, 2022

@phargogh I'd like to squash these commits before merging

Copy link
Member

@phargogh phargogh left a comment

Choose a reason for hiding this comment

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

Looks great, thanks@emlys !

@phargogh phargogh merged commit 05525e9 into natcap:main Jul 14, 2022
@emlys emlys deleted the task/967 branch March 8, 2023 01:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use PEP 420: implicit namespace packages
2 participants