Python - mypy attribute error for `fractions` library attribute

82 Views Asked by At

I'm wondering if this is a simply mypy config issue, but I'm getting the following error when running mypy against some libraries, some of which use fractions._RATIONAL_FORMAT module attribute, which happens to be a compiled regular expression (re.Pattern).

The mypy error actually says that it is not:

error: Module "fractions" has no attribute "_RATIONAL_FORMAT"  [attr-defined]

Clearly _RATIONAL_FORMAT is a fractions module attribute:

>>> import fractions
>>> getattr(fractions, '_RATIONAL_FORMAT')
re.compile(r'\n    \A\s*                                 # optional whitespace at the start,\n    (?P<sign>[-+]?)                       # an optional sign, then\n    (?=\d|\.\d)                           # lookahead for digit or .digit\n    (?P<num>\d*|\d+(_\d+)*)               # numerator (possibly empty)\n    (?:                                   # followed by\n       (?:/(?P<denom>\d+(_\d+)*))?        # an optional denominator\n    |                                     # or\n       (?:\.(?P<decimal>d*|\d+(_\d+)*))?  # an optional fractional part\n       (?:E(?P<exp>[-+]?\d+(_\d+)*))?     # and optional exponent\n    )\n    \s*\Z                                 # and optional whitespace to finish\n',
           re.IGNORECASE|re.UNICODE|re.VERBOSE)

I saw some relevant discussion on this type of error in the mypy repo, but the solution proposed there does not work for me - it was to rewrite all imports as aliased imports (using as), and also setting the config. option implicit_reexport to true in the TOML.

My [tool.mypy] section in the project TOML looks like this:

[tool.mypy]
python_version = 3.12
mypy_path = "src"
namespace_packages = true
disallow_incomplete_defs = true
disallow_untyped_defs = true
disallow_untyped_decorators = true
exclude = "version.py"
explicit_package_bases = true
follow_imports = "silent"
ignore_missing_imports = true
implicit_reexport = true

I'm using mypy version 1.9.0 and Python 3.11.2.

0

There are 0 best solutions below