Skip to content

Cope with Pycharm's oddity in detecting namespace packages - #43116

Closed
ashb wants to merge 1 commit into
mainfrom
pycarm-ns-pakage-fix
Closed

Cope with Pycharm's oddity in detecting namespace packages#43116
ashb wants to merge 1 commit into
mainfrom
pycarm-ns-pakage-fix

Conversation

@ashb

@ashbashb commented Oct 17, 2024

Copy link
Copy Markdown
Member

Without the __path__ assignment being the very first line of the file
(including before any import) it doesn't detect this correctly.

Without the `__path__` assignment being the very first line of the file
(including before any import) it doesn't detect this correctly.
@ashb

ashb commented Oct 17, 2024

Copy link
Copy Markdown
MemberAuthor

@shahar1@kaxil Could one of you test if this fixes it please?

@kaxil

Copy link
Copy Markdown
Member

@shahar1@kaxil Could one of you test if this fixes it please?

Yup, works now

Comment threadairflow/__init__.py
# 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Although:

Python mandates that future-imports must appear in the module before any other code except docstrings

https://peps.python.org/pep-0008/#module-level-dunder-names

@shahar1shahar1Oct 17, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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...

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

This really does feel like a massive bug in pycharm :(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🐼☹

Comment threadairflow/__init__.py
# 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

@shahar1shahar1Oct 17, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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...

@vincbeck

Copy link
Copy Markdown
Contributor

Yeah I tried in #43081 but could not make it work. Mypy/Python is not happy about it and raise a SyntaxError

@ashb

ashb commented Oct 17, 2024

Copy link
Copy Markdown
MemberAuthor

@potiuk

potiuk commented Oct 18, 2024

Copy link
Copy Markdown
Member

Let's just exclude airflow.__init__.py from automatically adding from __future__ import annotations and convert it back to use "regular" typing.

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

potiuk added a commit to potiuk/airflow that referenced this pull request Oct 18, 2024
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.
@potiuk

Copy link
Copy Markdown
Member

I added alternative implementation - and it seems to work for me #43173 - just by removing from future and adding ignore rules from ruff seem to do the job as I suspected. Actually airflow/__init__.py did not require any change beceause it does not use postponed evaluation of types PEP563 at all.

potiuk added a commit to potiuk/airflow that referenced this pull request Oct 18, 2024
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:
* 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)
potiuk added a commit to potiuk/airflow that referenced this pull request Oct 18, 2024
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)
kaxil pushed a commit to potiuk/airflow that referenced this pull request Oct 18, 2024
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)
kaxil added a commit that referenced this pull request Oct 18, 2024
…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 <kaxilnaik@gmail.com>
harjeevanmaan pushed a commit to harjeevanmaan/airflow that referenced this pull request Oct 23, 2024
…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 <kaxilnaik@gmail.com>
PaulKobow7536 pushed a commit to PaulKobow7536/airflow that referenced this pull request Oct 24, 2024
…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 <kaxilnaik@gmail.com>
@potiuk

Copy link
Copy Markdown
Member

Closing this one. It's already fixed.

@potiukpotiuk closed this Oct 29, 2024
ellisms pushed a commit to ellisms/airflow that referenced this pull request Nov 13, 2024
…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 <kaxilnaik@gmail.com>
@potiuk
potiuk deleted the pycarm-ns-pakage-fix branch January 11, 2025 19:44
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@ashb@kaxil@vincbeck@potiuk@shahar1