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

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

Closed
wants to merge 1 commit into from

Conversation

ashb
Copy link
Member

@ashb ashb commented Oct 17, 2024

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
Copy link
Member Author

ashb commented Oct 17, 2024

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

@kaxil
Copy link
Member

kaxil commented Oct 17, 2024

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

Yup, works now

__path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore

from __future__ import annotations # noqa: F404
Copy link
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

Copy link
Contributor

@shahar1 shahar1 Oct 17, 2024

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
Member Author

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

Choose a reason for hiding this comment

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

🐼☹

__path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore

from __future__ import annotations # noqa: F404
Copy link
Contributor

@shahar1 shahar1 Oct 17, 2024

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
Contributor

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

@ashb
Copy link
Member Author

ashb commented Oct 17, 2024

@potiuk
Copy link
Member

potiuk commented Oct 18, 2024

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
Copy link
Member

potiuk commented Oct 18, 2024

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.

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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
@potiuk
Copy link
Member

potiuk commented Oct 29, 2024

Closing this one. It's already fixed.

@potiuk potiuk 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 <[email protected]>
@potiuk potiuk deleted the pycarm-ns-pakage-fix branch January 11, 2025 19:44
Sign up for free to 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