Cope with Pycharm's oddity in detecting namespace packages#43116
Cope with Pycharm's oddity in detecting namespace packages#43116
Conversation
Without the `__path__` assignment being the very first line of the file (including before any import) it doesn't detect this correctly.
| # And it _has_ to be the first code line too. Sad panda | ||
| __path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore | ||
|
|
||
| from __future__ import annotations # noqa: F404 |
There was a problem hiding this comment.
Although:
Python mandates that future-imports must appear in the module before any other code except docstrings
There was a problem hiding this comment.
Question to the audience - can we live with it (or similar workaround) for now until we find a better alternative?
Partial answer - mypy and some static checks don't like it...
There was a problem hiding this comment.
This really does feel like a massive bug in pycharm :(
There was a problem hiding this comment.
Yeah, for now I think we can live with that bug.
Once we move a good number of things from Airflow to SDK, maybe the airflow/__init__.py file won't have much content left to keep from __future__ import annotations!
| # Make `airflow` a namespace package, supporting installing | ||
| # airflow.providers.* in different locations (i.e. one in site, and one in user | ||
| # lib.) This is required by some IDEs to resolve the import paths. | ||
| # And it _has_ to be the first code line too. Sad panda |
| # And it _has_ to be the first code line too. Sad panda | ||
| __path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore | ||
|
|
||
| from __future__ import annotations # noqa: F404 |
There was a problem hiding this comment.
Question to the audience - can we live with it (or similar workaround) for now until we find a better alternative?
Partial answer - mypy and some static checks don't like it...
|
Yeah I tried in #43081 but could not make it work. Mypy/Python is not happy about it and raise a |
|
https://youtrack.jetbrains.com/issue/PY-46690/Allow-all-imports-from-the-same-named-packages-if-path-manipulation-in-init-is-detected is the closest current issue I've found, but not exactly the same |
|
Let's just exclude We've done that for Pydantic classes before and still doing it for some test files https://github.com/apache/airflow/blob/main/pyproject.toml#L355 |
|
I added alternative implementation - and it seems to work for me #43173 - just by removing |
Cleans-up airflow and providers `__init__.py" files in order to get providers import work again. This is done by excluding the two `__init__.py` files from automated ruff isort rules adding `from __future__ import annotations`. Also removed the `__init__.py` file from "providers" directory, it is not needed there, because "providers" is just a folder where we keep provider files, it's not a Python package. That should finally get rid of the Intellij teething import problem that has been introduced in apache#42505. There were earlier - unsuccesful - attempts to fix it in the apache#43116 and apache#43081 that followed apache#42951 - but the key is that Pycharm requires the namespace's extend_path to be first "real" line of code in the `__init__.py` to understand that the package is an "explicit" namespace package - and it conflicts with the requirement of "from __future__ import annotations" to be the first line of Python code. Also this PR fixes a few other teething problems with setup of tests that were introcuded in apache#42505 and apache#43802 "masked" by having `__init__.py` added in providers package: * common.sql interface pre-commit used wrong path to generated files * openlineage extractor test that should not expect "providers.tests.*" but "tests.*" package * common_sql_api_stubs wrongly calculating generated path for stub-generated files * pytest_plugin expecting .asf.yml in "airflow" sources - even during compatibility tests with older version of airflow (where the .asf.yml is not present)
…3173) Cleans-up airflow and providers `__init__.py" files in order to get providers import work again. This is done by excluding the two `__init__.py` files from automated ruff isort rules adding `from __future__ import annotations`. That should finally get rid of the Intellij teething import problem that has been introduced in #42505. There were earlier - unsuccessful - attempts to fix it in the #43116 and #43081 that followed #42951 - but the key is that Pycharm requires the namespace's extend_path to be first "real" line of code in the `__init__.py` to understand that the package is an "explicit" namespace package - and it conflicts with the requirement of "from __future__ import annotations" to be the first line of Python code. Also this PR fixes following problem: * pytest_plugin expecting .asf.yml in "airflow" sources - even during compatibility tests with older version of airflow (where the .asf.yml is not present) --------- Co-authored-by: Kaxil Naik <[email protected]>
…ache#43173) Cleans-up airflow and providers `__init__.py" files in order to get providers import work again. This is done by excluding the two `__init__.py` files from automated ruff isort rules adding `from __future__ import annotations`. That should finally get rid of the Intellij teething import problem that has been introduced in apache#42505. There were earlier - unsuccessful - attempts to fix it in the apache#43116 and apache#43081 that followed apache#42951 - but the key is that Pycharm requires the namespace's extend_path to be first "real" line of code in the `__init__.py` to understand that the package is an "explicit" namespace package - and it conflicts with the requirement of "from __future__ import annotations" to be the first line of Python code. Also this PR fixes following problem: * pytest_plugin expecting .asf.yml in "airflow" sources - even during compatibility tests with older version of airflow (where the .asf.yml is not present) --------- Co-authored-by: Kaxil Naik <[email protected]>
…ache#43173) Cleans-up airflow and providers `__init__.py" files in order to get providers import work again. This is done by excluding the two `__init__.py` files from automated ruff isort rules adding `from __future__ import annotations`. That should finally get rid of the Intellij teething import problem that has been introduced in apache#42505. There were earlier - unsuccessful - attempts to fix it in the apache#43116 and apache#43081 that followed apache#42951 - but the key is that Pycharm requires the namespace's extend_path to be first "real" line of code in the `__init__.py` to understand that the package is an "explicit" namespace package - and it conflicts with the requirement of "from __future__ import annotations" to be the first line of Python code. Also this PR fixes following problem: * pytest_plugin expecting .asf.yml in "airflow" sources - even during compatibility tests with older version of airflow (where the .asf.yml is not present) --------- Co-authored-by: Kaxil Naik <[email protected]>
|
Closing this one. It's already fixed. |
…ache#43173) Cleans-up airflow and providers `__init__.py" files in order to get providers import work again. This is done by excluding the two `__init__.py` files from automated ruff isort rules adding `from __future__ import annotations`. That should finally get rid of the Intellij teething import problem that has been introduced in apache#42505. There were earlier - unsuccessful - attempts to fix it in the apache#43116 and apache#43081 that followed apache#42951 - but the key is that Pycharm requires the namespace's extend_path to be first "real" line of code in the `__init__.py` to understand that the package is an "explicit" namespace package - and it conflicts with the requirement of "from __future__ import annotations" to be the first line of Python code. Also this PR fixes following problem: * pytest_plugin expecting .asf.yml in "airflow" sources - even during compatibility tests with older version of airflow (where the .asf.yml is not present) --------- Co-authored-by: Kaxil Naik <[email protected]>
Without the
__path__assignment being the very first line of the file(including before any import) it doesn't detect this correctly.