incalculable-hydrogen-44003
09/06/2022, 10:07 PMsparse-lifeguard-95737
09/06/2022, 10:10 PMdef django_app_sources(
*,
name: Optional[str] = None,
models_target: Optional[str] = "./models.py",
with_migrations: bool = True,
with_admin: bool = True,
with_translation: bool = False,
with_templates: bool = True,
with_fixtures: bool = False,
with_content_fixtures: bool = False,
app_dependencies: Optional[List[str]] = None,
**python_sources_kwargs
):
"""Target for the top-level sources of a Django app.
Registers 2 targets in the directory:
1. A `python_source` covering `apps.py`
2. A `python_sources` covering all other `*.py` files at the top level
The `apps.py` target is registered as the default in the directory, to help
with pants inference based on references from Django settings. All other top-
level sources are marked as dependencies of `apps.py`, to ensure `urls.py`,
`signals.py`, etc. are pulled in when their `apps.py` is pulled.
By default, the top-level `./migrations` directory is also marked as a dependency
of `apps.py`. Set `with_migrations=False` for apps without migrations to prevent
errors from pants.
All other `kwargs` passed to the macro are passed through to the underlying
`python_sources()` target.
"""
if "sources" not in python_sources_kwargs:
python_sources_kwargs["sources"] = []
python_sources_kwargs["sources"].extend(["*.py", "!apps.py"])
if not app_dependencies:
app_dependencies = []
if models_target:
app_dependencies.append(models_target)
if with_migrations:
app_dependencies.append("./migrations")
if with_admin:
# `admin.py` is magically auto-loaded when `django.contrib.admin` is installed.
app_dependencies.append("./admin.py")
if with_translation:
# `translation.py` is magically auto-loaded when `modeltranslation` is installed.
app_dependencies.append("./translation.py")
if with_templates:
app_dependencies.append("./templates")
if with_fixtures:
app_dependencies.append("./fixtures")
if with_content_fixtures:
app_dependencies.append("./content_fixtures")
if not name:
name = "app"
python_source(name=name, source="apps.py", dependencies=app_dependencies)
python_sources(**python_sources_kwargs)
apps.py
with an AppConfig
subclass, and updated our INSTALLED_APPS
settings to refer to the AppConfig
subclasses instead of using the short-hand and pointing at the app modulesincalculable-hydrogen-44003
09/06/2022, 10:13 PMbusy-vase-39202
09/06/2022, 10:14 PMsparse-lifeguard-95737
09/06/2022, 10:14 PM[python-infer].string_imports = true
in pants.toml
makes inference work from INSTALLED_APPS
-> the actual app codeincalculable-hydrogen-44003
09/06/2022, 10:16 PMsparse-lifeguard-95737
09/06/2022, 10:17 PMpython_requirements
?incalculable-hydrogen-44003
09/06/2022, 10:18 PMsparse-lifeguard-95737
09/06/2022, 10:23 PMstring_imports_min_dots
might be too high to pick up on some 3rd-party apps (i.e. the django-impersonate
package provides the impersonate
app, which won’t pass the default min-dots test). you could try setting the value to 0 to catch everything, but then pants will be checking every string in your code which I assume will be noticeably slow: https://www.pantsbuild.org/docs/reference-python-infer#section-string-imports-min-dots
2. if the apps have enough dots but the import path doesn’t match the package name, you might need to set the module_mapping
on your python_requirements
target: https://www.pantsbuild.org/docs/reference-python_requirements#codemodule_mappingcodeincalculable-hydrogen-44003
09/06/2022, 10:32 PMstrong-toothbrush-37759
09/23/2022, 9:34 AMsparse-lifeguard-95737
09/23/2022, 4:19 PMmacros.py
file, registered using these instructions: https://www.pantsbuild.org/docs/macros#how-to-add-a-macro