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

For Python >= 3.11 directly use packaging to compare package versions #1416

Merged
merged 2 commits into from
Sep 12, 2023

Conversation

blag
Copy link
Contributor

@blag blag commented Sep 7, 2023

Description

On Python 3.11 and beyond, pkg_resources no longer bundles packaging:

$ python3.11
Python 3.11.4 (main, Jul 25 2023, 17:35:35)
Type "help", "copyright", "credits" or "license" for more information.
>>> import pkg_resources
>>> pkg_resources.packaging
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'pkg_resources' has no attribute 'packaging'
>>>

That causes this issue in pgcli:

Traceback (most recent call last):
  File ".../bin/pgcli", line 5, in <module>
    from pgcli.main import cli
  File ".../lib/python3.11/site-packages/pgcli/main.py", line 52, in <module>
    from .pgtoolbar import create_toolbar_tokens_func
  File ".../lib/python3.11/site-packages/pgcli/pgtoolbar.py", line 1, in <module>
    from pkg_resources import packaging
ImportError: cannot import name 'packaging' from 'pkg_resources' (unknown location)

So for Python 3.11+, we install the packaging...package, and use it directly:

try:
    from packaging.version import parse as parse_version
except ImportError:
    from pkg_resources.packaging.version import parse as parse_version

Checklist

  • I've added this contribution to the changelog.rst.
  • I've added my name to the AUTHORS file (or it's already there).
  • I installed pre-commit hooks (pip install pre-commit && pre-commit install), and ran black on my code.
  • Please squash merge this pull request (uncheck if you'd like us to merge as multiple commits)

@j-bennet
Copy link
Contributor

j-bennet commented Sep 7, 2023

Hi @blag , thank you for improving pgcli! I see some errors in CI, would you be able to address those? Let me know if you need any help.

@dbaty
Copy link
Member

dbaty commented Sep 9, 2023

This is only used to detect the version of prompt_toolkit in the following block :

vi_modes = {
    InputMode.INSERT: "I",
    InputMode.NAVIGATION: "N",
    InputMode.REPLACE: "R",
    InputMode.INSERT_MULTIPLE: "M",
}
if parse_version(prompt_toolkit.__version__) >= parse_version("3.0.6"):
    vi_modes[InputMode.REPLACE_SINGLE] = "R"

We could surely check the existence of InputMode.REPLACE_SINGLE instead of explictly checking the version? Something like this:

vi_modes = {...}
if "REPLACE_SINGLE" in {e.name for e in InputMode}:  # available in prompt_toolkit >= 3.0.6 
    vi_modes[InputMode.REPLACE_SINGLE] = "R"

(or use a try/except clause, or InputMode._member_names_ if we're feeling slightly adventurous...)

@blag
Copy link
Contributor Author

blag commented Sep 9, 2023

@j-bennet CI run now needs approval. But I removed packaging from setup.py, so the dependency installation issues should now be moot.

@dbaty Thanks for the fleshed-out suggestion, I went with it.

@codecov-commenter
Copy link

Codecov Report

Patch coverage: 51.70% and project coverage change: -4.66% ⚠️

Comparison is base (6884c29) 84.15% compared to head (05d0cc2) 79.50%.
Report is 61 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1416      +/-   ##
==========================================
- Coverage   84.15%   79.50%   -4.66%     
==========================================
  Files          21       25       +4     
  Lines        2720     2981     +261     
==========================================
+ Hits         2289     2370      +81     
- Misses        431      611     +180     
Files Changed Coverage Δ
pgcli/completion_refresher.py 91.76% <ø> (+1.17%) ⬆️
pgcli/magic.py 0.00% <0.00%> (ø)
pgcli/packages/parseutils/tables.py 97.67% <ø> (ø)
pgcli/packages/sqlcompletion.py 97.67% <ø> (ø)
pgcli/pgcompleter.py 96.95% <ø> (ø)
pgcli/pgstyle.py 64.00% <ø> (ø)
pgcli/pyev.py 15.38% <15.38%> (ø)
pgcli/pgtoolbar.py 25.71% <20.00%> (-5.72%) ⬇️
pgcli/explain_output_formatter.py 46.15% <46.15%> (ø)
pgcli/key_bindings.py 52.94% <66.66%> (-0.19%) ⬇️
... and 8 more

... and 1 file with indirect coverage changes

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

@j-bennet
Copy link
Contributor

Looks good @blag , merging. 👍

@j-bennet j-bennet merged commit ed89c15 into dbcli:main Sep 12, 2023
7 checks passed
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

4 participants