hatch: AttributeError: 'SafeApplication' object has no attribute 'project'
I wasn’t sure what to title this issue, but it seems to be a regression because I feel like it wasn’t happening with the previous version of hatch.
If I have a custom environment, let’s say docs, that inherits from the default environment which was not created beforehand, and I try to run a command in the docs environment, it will fail with the error below.
This issue still happens, for example, if I have two custom environments, dev and test, with test inheriting from dev, if the dev environment is not created beforehand, then trying to run a command in the test environment will result in the same error. If I’m not mistaken, previously hatch would create the base environment if there is one before trying to run the command.
Run hatch run test:test
Syncing environment plugin requirements
Creating environment: test
Installing project in development mode
╭───────────────────── Traceback (most recent call last) ──────────────────────╮
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/hatch/cl │
│ i/__init__.py:221 in main │
│ │
│ 218 │
│ 219 def main(): # no cov │
│ 220 │ try: │
│ ❱ 221 │ │ hatch(prog_name='hatch', windows_expand_args=False) │
│ 222 │ except Exception: # noqa: BLE001 │
│ 223 │ │ import sys │
│ 224 │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/click/co │
│ re.py:1157 in __call__ │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/click/co │
│ re.py:1078 in main │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/click/co │
│ re.py:1688 in invoke │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/click/co │
│ re.py:1434 in invoke │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/click/co │
│ re.py:783 in invoke │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/click/de │
│ corators.py:33 in new_func │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/hatch/cl │
│ i/run/__init__.py:141 in run │
│ │
│ [13](https://github.com/Tobi-De/pmprep/actions/runs/8965839700/job/24620124542#step:6:14)8 │ elif not env_name: │
│ 139 │ │ env_name = 'system' │
│ [14](https://github.com/Tobi-De/pmprep/actions/runs/8965839700/job/24620124542#step:6:15)0 │ │
│ ❱ 141 │ ctx.invoke( │
│ 142 │ │ run_command, │
│ 143 │ │ args=[command, *final_args], │
│ 144 │ │ env_names=[env_name], │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/click/co │
│ re.py:783 in invoke │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/click/de │
│ corators.py:45 in new_func │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/hatch/cl │
│ i/env/run.py:128 in run │
│ │
│ 125 │ elif not matrix_selected and (included_variables or excluded_varia │
│ 126 │ │ app.abort(f'Variable selection is unsupported for non-matrix e │
│ 127 │ │
│ ❱ 128 │ for context in app.runner_context( │
│ 129 │ │ environments, │
│ 130 │ │ ignore_compat=ignore_compat or matrix_selected, │
│ 131 │ │ display_header=matrix_selected, │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/hatch/cl │
│ i/application.py:224 in runner_context │
│ │
│ 221 │ │ │ │ context = ExecutionContext(environment) │
│ 222 │ │ │ │ yield context │
│ 223 │ │ │ │ │
│ ❱ 224 │ │ │ │ self.prepare_environment(environment) │
│ 225 │ │ │ │ with EnvVars(context.env_vars): │
│ 226 │ │ │ │ │ self.run_shell_commands(context) │
│ 227 │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/hatch/cl │
│ i/application.py:143 in prepare_environment │
│ │
│ 140 │ │ │ │ │ │ ) │
│ 141 │ │ │
│ 142 │ │ with environment.app_status_dependency_state_check(): │
│ ❱ 143 │ │ │ new_dep_hash = environment.dependency_hash() │
│ 144 │ │ │
│ 145 │ │ current_dep_hash = self.env_metadata.dependency_hash(environme │
│ 146 │ │ if new_dep_hash != current_dep_hash: │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/hatch_pi │
│ p_compile/plugin.py:104 in dependency_hash │
│ │
│ 101 │ │ """ │
│ 102 │ │ Get the dependency hash │
│ 103 │ │ """ │
│ ❱ 104 │ │ self.run_pip_compile() │
│ 105 │ │ hatch_hash = super().dependency_hash() │
│ 106 │ │ if not self.dependencies: │
│ 107 │ │ │ return hatch_hash │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/hatch_pi │
│ p_compile/plugin.py:117 in run_pip_compile │
│ │
│ 114 │ │ Run pip-compile if necessary │
│ 1[15](https://github.com/Tobi-De/pmprep/actions/runs/8965839700/job/24620124542#step:6:16) │ │ """ │
│ 1[16](https://github.com/Tobi-De/pmprep/actions/runs/8965839700/job/24620124542#step:6:17) │ │ self.prepare_environment() │
│ ❱ 117 │ │ if not self.lockfile_up_to_date: │
│ 118 │ │ │ with self.safe_activation(): │
│ 119 │ │ │ │ self.resolver.install_pypi_dependencies() │
│ 120 │ │ │ │ if self.piptools_lock_file.exists(): │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/functools.py:995 in │
│ __get__ │
│ │
│ 992 │ │ │ raise TypeError(msg) from None │
│ 993 │ │ val = cache.get(self.attrname, _NOT_FOUND) │
│ 994 │ │ if val is _NOT_FOUND: │
│ ❱ 995 │ │ │ val = self.func(instance) │
│ 996 │ │ │ try: │
│ 997 │ │ │ │ cache[self.attrname] = val │
│ 998 │ │ │ except TypeError: │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/hatch_pi │
│ p_compile/plugin.py:200 in lockfile_up_to_date │
│ │
│ 197 │ │ if not self.dependencies and not self.piptools_lock_file.exist │
│ 198 │ │ │ return True │
│ 199 │ │ if self.piptools_constraints_file: │
│ ❱ 200 │ │ │ valid_constraint = self.validate_constraints_file( │
│ 201 │ │ │ │ constraints_file=self.piptools_constraints_file, envir │
│ 202 │ │ │ ) │
│ 203 │ │ │ if not valid_constraint: │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/hatch_pi │
│ p_compile/plugin.py:325 in validate_constraints_file │
│ │
│ 322 │ │ │ │ requirements=environment.dependencies_complex │
│ 323 │ │ │ ) │
│ 324 │ │ │ if not up_to_date: │
│ ❱ 325 │ │ │ │ self.constraint_env.run_pip_compile() │
│ 326 │ │ │ │ return False │
│ 327 │ │ return True │
│ 328 │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/hatch_pi │
│ p_compile/plugin.py:116 in run_pip_compile │
│ │
│ 113 │ │ """ │
│ 114 │ │ Run pip-compile if necessary │
│ 115 │ │ """ │
│ ❱ 116 │ │ self.prepare_environment() │
│ 1[17](https://github.com/Tobi-De/pmprep/actions/runs/8965839700/job/24620124542#step:6:18) │ │ if not self.lockfile_up_to_date: │
│ 118 │ │ │ with self.safe_activation(): │
│ 119 │ │ │ │ self.resolver.install_pypi_dependencies() │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/hatch_pi │
│ p_compile/plugin.py:367 in prepare_environment │
│ │
│ 364 │ │ but the `Application` class is not exposed to the environment │
│ 365 │ │ """ │
│ 366 │ │ if not self.virtualenv_exists(): │
│ ❱ 367 │ │ │ self.create() │
│ 368 │ │ │ if not self.dependencies_in_sync(): │
│ 369 │ │ │ │ self.sync_dependencies() │
│ 370 │ │ │ if not self.skip_install: │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/hatch/en │
│ v/virtual.py:157 in create │
│ │
│ 154 """ │
│ 155 │ │ │ │ ) │
│ 156 │ │ │
│ ❱ 157 │ │ with self.expose_uv(): │
│ 158 │ │ │ self.virtual_env.create(self.parent_python, allow_system_p │
│ 159 │ │
│ 160 │ def remove(self): │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/hatch/en │
│ v/virtual.py:99 in expose_uv │
│ │
│ 96 │ │ return UVVirtualEnv if self.use_uv else VirtualEnv │
│ 97 │ │
│ 98 │ def expose_uv(self): │
│ ❱ 99 │ │ if not (self.use_uv or self.uv_path): │
│ 100 │ │ │ return nullcontext() │
│ 101 │ │ │
│ 102 │ │ return EnvVars({'HATCH_UV': self.uv_path}) │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/functools.py:995 in │
│ __get__ │
│ │
│ 992 │ │ │ raise TypeError(msg) from None │
│ 993 │ │ val = cache.get(self.attrname, _NOT_FOUND) │
│ 994 │ │ if val is _NOT_FOUND: │
│ ❱ 995 │ │ │ val = self.func(instance) │
│ 996 │ │ │ try: │
│ 997 │ │ │ │ cache[self.attrname] = val │
│ 998 │ │ │ except TypeError: │
│ │
│ /opt/hostedtoolcache/Python/3.12.3/x64/lib/python3.12/site-packages/hatch/en │
│ v/virtual.py:116 in uv_path │
│ │
│ 113 │ │ │ # Prevent recursive loop │
│ 114 │ │ │ self.name == env_name │
│ 115 │ │ │ # Only if dependencies have been set by the user │
│ ❱ 116 │ │ │ or is_default_environment(env_name, self.app.project.confi │
│ 117 │ │ ): │
│ 1[18](https://github.com/Tobi-De/pmprep/actions/runs/8965839700/job/24620124542#step:6:19) │ │ │ uv_env = self.app.get_environment(env_name) │
│ 1[19](https://github.com/Tobi-De/pmprep/actions/runs/8965839700/job/24620124542#step:6:20) │ │ │ self.app.prepare_environment(uv_env) │
╰──────────────────────────────────────────────────────────────────────────────╯
AttributeError: 'SafeApplication' object has no attribute 'project'
Error: Process completed with exit code 1.
This is part of the pyproject.toml of the project that generate the error above, I’m using hatch-pip-compile but I doubt it has anything to do with this issue.
I remove the dependencies key since it was not relevant
[tool.hatch.envs.default]
type = "pip-compile"
pip-compile-constraint = "default"
pip-compile-installer = "uv"
pip-compile-resolver = "uv"
[tool.hatch.envs.dev]
lock-filename = "requirements-dev.txt"
[tool.hatch.envs.dev.scripts]
runserver = ["migrate", "python manage.py tailwind runserver {args}"]
migrate = "python manage.py migrate {args}"
makemigrations = "python manage.py makemigrations {args}"
reset-db = "python manage.py reset_db --noinput"
shell = "python manage.py shell_plus {args}"
[tool.hatch.envs.docs]
lock-filename = "docs/requirements.txt"
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 20 (16 by maintainers)
Commits related to this issue
- 🐛 hatch compatibility layer https://github.com/juftin/hatch-pip-compile/pull/80 https://github.com/pypa/hatch/issues/1468 — committed to juftin/hatch-pip-compile by juftin 2 years ago
- 🐛 hatch compatibility layer https://github.com/juftin/hatch-pip-compile/pull/80 https://github.com/pypa/hatch/issues/1468 — committed to juftin/hatch-pip-compile by juftin 2 years ago
- 🐛 hatch compatibility layer https://github.com/juftin/hatch-pip-compile/pull/80 https://github.com/pypa/hatch/issues/1468 — committed to juftin/hatch-pip-compile by juftin 2 years ago
Oh great, thank you! Please link that here when it’s open so I can copy for my tests lol
Thanks for the mention @ofek 🙏 @Tobi-De I will get a look at this as soon as I can and follow up with some debugging steps once I have an idea. I’m actually on a business / vacation trip right now in Europe from the US and a bit jet-lagged so no promises on exact ETA to resolution, I’ll do my best for something quick.
I was annoyed at my own procrastination so I updated every test and fixed the property https://github.com/pypa/hatch/pull/1546
Can this be closed?
😅 I’m still out on vacation but it’s looking like hatch
1.10.xcreated a regression withhatch-pip-compile. Ultimately the issue occurs when an environment that doesn’t exist tries to create itself.I think this has to do with the new
uvfunctionality - it’s got something to do withhatch-pip-compilereceiving ahatchling.bridge.app.Applicationwhen it actually is expecting ahatch.cli.Application: https://github.com/pypa/hatch/blob/7986421e531d37155cfabd615546c4cdc658e385/src/hatch/env/plugin/interface.py#L93-L103I’m debugging on a train in Portugal right now and it’s slow going… I’ll continue to provide more details.
EDIT I’m working on this over at https://github.com/juftin/hatch-pip-compile/pull/80 where I’ve got a minimal reproducible example