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

Issue with importing format_compact_decimal (Babel 2.11.0) #920

Closed
DenverCoder1 opened this issue Nov 1, 2022 · 7 comments
Closed

Issue with importing format_compact_decimal (Babel 2.11.0) #920

DenverCoder1 opened this issue Nov 1, 2022 · 7 comments
Assignees

Comments

@DenverCoder1
Copy link
Contributor

Overview Description

In some places, there is an issue when trying to to import and use format_compact_decimal.

It does not matter if from babel.numbers import format_compact_decimal is used or from babel import numbers.

When trying to import into a project, it shows an AttributeError, but when running in the Python REPL it works fine.

When running tox it seems to work, but when running pyright it does not.

Internally, within Babel files, the import is working as is evident by the tests passing.

The Babel documentation seems to have not imported the function successfully.

This is strange since I can't seem to find any reason format_compact_decimal would behave differently than other functions in the module, for example format_decimal, format_percent, etc.

In all cases, the latest version of the code is used, Babel 2.11.0, tested with Python 3.10.

Error in Pyright:

Error: "format_compact_decimal" is not a known member of module (reportGeneralTypeIssues)

Pyright Checks failing

https://github.com/DenverCoder1/github-readme-youtube-cards/actions/runs/3370558404/jobs/5591614482

Warning in VS Code

image

Warning appearing when trying to build docs (make -C docs html):

WARNING: autodoc: failed to import function 'format_compact_decimal' from module 'babel.numbers'; the following exception was raised:
Traceback (most recent call last):
  File "/usr/local/python/3.10.4/lib/python3.10/site-packages/sphinx/util/inspect.py", line 448, in safe_getattr
    return getattr(obj, name, *defargs)
AttributeError: module 'babel.numbers' has no attribute 'format_compact_decimal'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/python/3.10.4/lib/python3.10/site-packages/sphinx/ext/autodoc/importer.py", line 110, in import_object
    obj = attrgetter(obj, mangled_name)
  File "/usr/local/python/3.10.4/lib/python3.10/site-packages/sphinx/ext/autodoc/__init__.py", line 332, in get_attr
    return autodoc_attrgetter(self.env.app, obj, name, *defargs)
  File "/usr/local/python/3.10.4/lib/python3.10/site-packages/sphinx/ext/autodoc/__init__.py", line 2828, in autodoc_attrgetter
    return safe_getattr(obj, name, *defargs)
  File "/usr/local/python/3.10.4/lib/python3.10/site-packages/sphinx/util/inspect.py", line 464, in safe_getattr
    raise AttributeError(name) from exc
AttributeError: format_compact_decimal
@akx
Copy link
Member

akx commented Nov 2, 2022

Well, that's curious!

I don't get that error when building docs from the current master:

$ make -C docs html
sphinx-build -b html -d _build/doctrees   . _build/html
Running Sphinx v5.3.0
...
The HTML pages are in _build/html.
$

The built docs contain the new function just fine, too:

Screenshot 2022-11-02 at 7 53 26

However, it looks like RTD is experiencing the same issue and doesn't document the function. https://readthedocs.org/projects/python-babel/builds/18502285/

@akx
Copy link
Member

akx commented Nov 2, 2022

Oh.. 😂

Regarding docs, the issue is probably that Sphinx itself depends on Babel, and installing Sphinx can inadvertently pull in an older version of Babel.

Excerpt from the RTD log:

Collecting babel!=2.0,>=1.3
  Downloading Babel-2.10.3-py3-none-any.whl (9.5 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.5/9.5 MB 133.7 MB/s eta 0:00:00

@DenverCoder1
Copy link
Contributor Author

I guess that mostly explains it. It seems that installing other packages that require it can force it to a lower version. And depending on the environment, the version could actually sometimes not be 2.11.0.

$ pip show babel
Name: Babel
Version: 2.10.3
Summary: Internationalization utilities
Home-page: https://babel.pocoo.org/
Author: Armin Ronacher
Author-email: [email protected]
License: BSD
Location: /home/codespace/.local/lib/python3.10/site-packages/Babel-2.10.3-py3.10.egg
Requires: pytz
Required-by: jupyterlab_server, Sphinx

akx added a commit that referenced this issue Nov 2, 2022
* Use Python 3.11
* Use Sphinx 5 (and update extlinks configuration)
* Install Babel from the build directory

Refs #920
@akx
Copy link
Member

akx commented Nov 2, 2022

I suspect that also has to do with 2.11.0 not having propagated to every PyPI mirror just yet, and local caches and whatnot can also think 2.10.3 is the newest version.

@DenverCoder1
Copy link
Contributor Author

DenverCoder1 commented Nov 2, 2022

That's definitely possible. I suppose for now I can use # pyright: reportGeneralTypeIssues=false or just wait to see if it resolves itself on its own. At least the code runs properly.

akx added a commit that referenced this issue Nov 2, 2022
* Use Python 3.11
* Use Sphinx 5 (and update extlinks configuration)
* Use Python 3 intersphinx mapping
* Install Babel from the build directory

Refs #920
@akx
Copy link
Member

akx commented Nov 2, 2022

Also, it's possible that pyright is using types-babel (see #795 about getting native types in here) which hasn't been updated for 2.11...

akx added a commit that referenced this issue Nov 2, 2022
* Use Python 3.11
* Use Sphinx 5 (and update extlinks configuration)
* Use Python 3 intersphinx mapping
* Install Babel from the build directory

Refs #920
@akx
Copy link
Member

akx commented Nov 2, 2022

Anyway, I think this is resolved as "Babel wasn't coming from inside the house". 😁

@akx akx closed this as completed Nov 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants