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

Fix importing of namespace packages and built-in modules #227

Merged

Conversation

eltoder
Copy link
Contributor

@eltoder eltoder commented Mar 20, 2024

  • Namespace packages commonly don't use _NamespaceLoader, and it is an implementation detail in any case. The loader is then set to None. There is no need to inspect the loader anyway. Everything we need is on the spec. The unit test had a "work-around" for this, but this does not happen in real usage.

  • There are more built-in modules than just "builtins". The full list is available in sys.builtin_module_names[1].

  • It is also hard to know if a standard library module is built as an extension or a built-in. For example, in my Ubuntu 22.04's build of python 3.10 both _pickle and _elementtree modules are built-in[2]. Use a third-party module (ujson) for tests, which is guaranteed to be an extension module.

  • pydantic's __init__ is no longer an extension module, so use mypyc instead, which we already have through mypy.

[1] https://docs.python.org/3/library/sys.html#sys.builtin_module_names
[2] https://github.com/python/cpython/blob/519b2ae22b54760475bbf62b9558d453c703f9c6/PC/config.c#L87

@ariebovenberg
Copy link
Owner

Thanks @eltoder for taking the time to research this and come up with an improvement. I'll have a look soon to see if we can merge this and get a new release out.

I'm not sure why the mypy github action now fails. Looks like error filtering mechanism isn't working properly 🤔

@eltoder
Copy link
Contributor Author

eltoder commented Mar 20, 2024

@ariebovenberg I added another commit with type annotations for all tests functions. Let's see if this fixes mypy failures. I'm actually not sure how it works otherwise -- running mypy on tests fails for me locally without this change.

@eltoder
Copy link
Contributor Author

eltoder commented Mar 20, 2024

I figured out why -- the overrides in mypy.ini don't work without the __init__.py file in tests, which I deleted, because it is not needed otherwise. I can add it back instead of adding type annotations to tests. Please let me know.

@ariebovenberg
Copy link
Owner

ariebovenberg commented Mar 21, 2024

the overrides in mypy.ini don't work without the init.py file in tests, which I deleted

Ah yes—that explains. Having to add -> None: to all test_ functions was what I wanted to avoid with this setup 😅

decision: let's keep the __init__.py in tests and undo the test type annotations.

Copy link

codecov bot commented Mar 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (2c2561c) to head (30f57c0).

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #227   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            6         6           
  Lines          502       495    -7     
  Branches       104       103    -1     
=========================================
- Hits           502       495    -7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ariebovenberg ariebovenberg self-requested a review March 21, 2024 08:14
Copy link
Owner

@ariebovenberg ariebovenberg left a comment

Choose a reason for hiding this comment

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

See comment in PR thread

* Namespace packages commonly don't use `_NamespaceLoader`, and it is
  an implementation detail in any case. The loader is then set to None.
  There is no need to inspect the loader anyway. Everything we need is
  on the `spec`. The unit test had a "work-around" for this, but this
  does not happen in real usage.

* There are more built-in modules than just "builtins". The full list is
  available in `sys.builtin_module_names`[1]. However, we don't need to
  handle them specially.

* It is also hard to know if a standard library module is built as an
  extension or a built-in. For example, in my Ubuntu 22.04's build of
  python 3.10 both `_pickle` and `_elementtree` modules are built-in[2].
  Use a third-party module (`ujson`) for tests, which is guaranteed to
  be an extension module.

* pydantic's `__init__` is no longer an extension module, so use mypyc
  instead, which we already have through mypy.

[1] https://docs.python.org/3/library/sys.html#sys.builtin_module_names
[2] https://github.com/python/cpython/blob/519b2ae22b54760475bbf62b9558d453c703f9c6/PC/config.c#L87
@eltoder
Copy link
Contributor Author

eltoder commented Mar 21, 2024

@ariebovenberg Done.

Also, I realized that we no longer need the special logic for builtins, and in fact should not skip the location check as we did previously. I added a test for this.

Copy link
Owner

@ariebovenberg ariebovenberg left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks for your contribution 🙏

@ariebovenberg ariebovenberg merged commit eb42767 into ariebovenberg:main Mar 21, 2024
9 checks passed
@ariebovenberg
Copy link
Owner

version 0.18.0 is now out with this change 🚀

@eltoder eltoder deleted the feature/namespace-packages branch March 21, 2024 15:46
@eltoder
Copy link
Contributor Author

eltoder commented Mar 21, 2024

Thank you! I actually have another related change. I'll open a PR shortly 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants