Skip to content

perf: Avoid module-level importlib.util.find_spec #2391

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

Merged
merged 2 commits into from
May 4, 2025

Conversation

MarcoGorelli
Copy link
Member

What type of PR is this? (check all applicable)

  • πŸ’Ύ Refactor
  • ✨ Feature
  • πŸ› Bug Fix
  • πŸ”§ Optimization
  • πŸ“ Documentation
  • βœ… Test
  • 🐳 Other

Related issues

  • Related issue #<issue number>
  • Closes #<issue number>

Checklist

  • Code follows style guide (ruff)
  • Tests added
  • Documented the changes

If you have comments or can explain your changes, please do so below

@dangotbanned
Copy link
Member

dangotbanned commented Apr 16, 2025

@MarcoGorelli could we slow down a bit on this one?

My read on this, was that the import wasn't the issue?

pyinstaller/pyinstaller-hooks-contrib#902 (comment)

Sure, thanks - would it help if we just removed the runtime import from Narwhals anyway?

Sure, if you don't need it. (But dynamic import with find_spec is not really the problematic part - it is the metadata query. Because while our analysis will miss the reference to typing_extensions made via find_spec, there are plenty of direct import typing_extensions later on, which allow us to pick up the module - mostly because we do not handle TYPE_CHECKING as no-op codepath...).

You could also catch the importlib.metadata.PackageNotFoundError when calling importlib.metadata.version(_TYPING_EXTENSIONS) in narwhals-dev/narwhals@7059f5b/narwhals/_translate.py#L20 (blame).

I just wanna be sure I understand the issue fully first

Edit

I replied in (pyinstaller/pyinstaller-hooks-contrib#902 (comment))

@dangotbanned
Copy link
Member

dangotbanned commented Apr 16, 2025

@MarcoGorelli okay it seems we're all good! πŸŽ‰

pyinstaller/pyinstaller-hooks-contrib#902 (comment)

As far as I'm concerned, there's no need to change anything on your side.

I followed up and checked, the fix they've added is pretty standard and they've used it in lots of popular packages

@MarcoGorelli
Copy link
Member Author

sure, thanks, should we just address

You could also catch the importlib.metadata.PackageNotFoundError when calling importlib.metadata.version(_TYPING_EXTENSIONS) in

in case that affects anyone else?

@dangotbanned
Copy link
Member

sure, thanks, should we just address

You could also catch the importlib.metadata.PackageNotFoundError when calling importlib.metadata.version(_TYPING_EXTENSIONS) in

in case that affects anyone else?

@MarcoGorelli how would you feel about making that a sub-task of adding a _typing_compat module?

We could have that extra safety there, plus:

@MarcoGorelli
Copy link
Member Author

I don't really mind, but based on #2479 I still think we may want to consider removing this - the else branch you've added seems to cover pre-3.13 behaviour perfectly

@dangotbanned dangotbanned changed the title fix: Never import from typing_extensions at runtime perf: Avoid moudle-level importlib.util.find_spec May 4, 2025
@dangotbanned dangotbanned changed the title perf: Avoid moudle-level importlib.util.find_spec perf: Avoid module-level importlib.util.find_spec May 4, 2025
@dangotbanned dangotbanned linked an issue May 4, 2025 that may be closed by this pull request
Copy link
Member

@dangotbanned dangotbanned left a comment

Choose a reason for hiding this comment

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

I don't really mind, but based on #2479 I still think we may want to consider removing this

Thanks @MarcoGorelli!

(#2479) points towards a cost that we can only avoid by not doing the find_spec call.
Since a TypeVar must be defined at the module-level (tried to find a ref but had no luck πŸ˜”) the only option we have is to use the else branch.

Appreciate you bearing with me on this πŸ˜…

I'd still like to clean up the bits mentioned in (#2391 (comment)) soon - but we might as well get the perf boost in now 🎁

@MarcoGorelli
Copy link
Member Author

sure! thanks a tonne for your attention to detail, here and everywhere

@MarcoGorelli MarcoGorelli merged commit faf8456 into narwhals-dev:main May 4, 2025
31 checks passed
@dangotbanned dangotbanned mentioned this pull request May 20, 2025
62 tasks
dangotbanned added a commit that referenced this pull request May 20, 2025
- Mentioned in (#2391 (comment))
- Needed again for #2572
@dangotbanned dangotbanned mentioned this pull request May 20, 2025
10 tasks
MarcoGorelli pushed a commit that referenced this pull request May 21, 2025
* chore(typing): Add `_typing_compat.py`

- Mentioned in (#2391 (comment))
- Needed again for #2572

* refactor: Reuse `TypeVar` import

* refactor: Reuse `@deprecated` import

* refactor: Reuse `Protocol38` import

* docs: Add module-level docstring
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.

perf: minimise/remove overhead from _typing_extensions_has_pep_696
2 participants