Skip to content

Conversation

podgorniy94
Copy link
Contributor

PyBabel currently fails to work with Python 3.12 when using third-party extractors. Specifically, the extraction method for Jinja2 templates throws a ValueError:

[python: **.py]
[jinja2: templates/**.html]
ValueError: Unknown extraction method 'jinja2'

This issue does not occur with Python 3.11, where the same setup functions correctly.

Issue: #861

Refactored both checkers.py and extract.py to use importlib.metadata
instead of pkg_resources for better performance and compatibility with
modern Python versions.

Closes python-babel#861
@akx
Copy link
Member

akx commented Jul 3, 2024

I don't think all of our currently supported platforms have importlib.metadata, so this will likely fail CI, but we'll see...

@j123b567
Copy link

j123b567 commented Jul 3, 2024

@akx you are right, it should try pkg_resources first and if it is not there, then try importlib.metadata and if it is not there, then do the fallback to predefined list

Copy link
Member

@akx akx left a comment

Choose a reason for hiding this comment

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

As discussed, this needs a fallback for environments where importlib.metadata.entry_points() is not available.

@podgorniy94
Copy link
Contributor Author

Another fallback was needed, this time for importlib.metadata, specifically for entry_points().

Error message:
AttributeError: 'str' object has no attribute 'group'

According to the documentation:

Compatibility
The "selectable" entry points were introduced in importlib_metadata 3.6 and Python 3.10. Prior to those changes, entry_points accepted no parameters and always returned a dictionary of entry points, keyed by group. With importlib_metadata 5.0 and Python 3.12, entry_points always returns an EntryPoints object. See backports.entry_points_selectable for compatibility options.

Copy link

codecov bot commented Jul 10, 2024

Codecov Report

Attention: Patch coverage is 69.69697% with 10 lines in your changes missing coverage. Please review.

Project coverage is 90.87%. Comparing base (e0d1018) to head (8f9e1de).
Report is 17 commits behind head on master.

Files Patch % Lines
babel/messages/checkers.py 64.70% 6 Missing ⚠️
babel/messages/extract.py 75.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1093      +/-   ##
==========================================
- Coverage   90.99%   90.87%   -0.13%     
==========================================
  Files          26       26              
  Lines        4444     4481      +37     
==========================================
+ Hits         4044     4072      +28     
- Misses        400      409       +9     

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

Copy link
Member

@akx akx left a comment

Choose a reason for hiding this comment

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

Could the duplicated code be DRYed into a single _get_group_entrypoints(group, name=None) generator function that could live in e.g. babel/util.py (as a private name)?

@podgorniy94
Copy link
Contributor Author

podgorniy94 commented Jul 18, 2024

@akx I'm preparing fixes, as I see you are planning to finish the update. I needed to let you know that I'm working on it and plan to finish it

@akx
Copy link
Member

akx commented Jul 18, 2024

@podgorniy94 I wasn't sure you were doing that, so please just see #1102 instead – the commit has you as a co-author :)

@akx akx closed this in #1102 Jul 25, 2024
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.

3 participants