Skip to content

Stubgen giving error about self documenting expressions in f-strings #10791

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

Closed
cquick01 opened this issue Jul 9, 2021 · 1 comment · Fixed by #10907
Closed

Stubgen giving error about self documenting expressions in f-strings #10791

cquick01 opened this issue Jul 9, 2021 · 1 comment · Fixed by #10907
Labels
bug mypy got something wrong topic-stubgen

Comments

@cquick01
Copy link

cquick01 commented Jul 9, 2021

Bug Report

Hi, I am trying to run stubgen but am running into an error

Critical error during semantic analysis: path/to/script.py:140: error: f-string: self documenting expressions are only supported in Python 3.8 and greater

But, I'm running Python 3.9.6, and mypy 0.910 which should definitely support 3.8+ since mypy 0.800 added support for 3.9.

To Reproduce

Paths to binaries

(venv) $ which python
/home/cquick/test/venv/bin/python
(venv) $ which mypy
/home/cquick/test/venv/bin/mypy
(venv) $ which stubgen
/home/cquick/test/venv/bin/stubgen

Versions

(venv) $ python -V
Python 3.9.6

(venv) $ python -m pip install -U mypy
Requirement already satisfied: mypy in ./venv/lib/python3.9/site-packages (0.910)
Requirement already satisfied: typing-extensions>=3.7.4 in ./venv/lib/python3.9/site-packages (from mypy) (3.10.0.0)
Requirement already satisfied: toml in ./venv/lib/python3.9/site-packages (from mypy) (0.10.2)
Requirement already satisfied: mypy-extensions<0.5.0,>=0.4.3 in ./venv/lib/python3.9/site-packages (from mypy) (0.4.3)

(venv) $ mypy -V
mypy 0.910

Simple test script

(venv) $ cat test.py
#!/usr/bin/env python3

def foo(x):
    print(f"{x = }")

if __name__ == "__main__":
    foo(1)

Test script successfully prints self documenting expression

(venv) $ python test.py
x = 1

But running stubgen fails

(venv) $ stubgen test.py
Critical error during semantic analysis: test.py:4: error: f-string: self documenting expressions are only supported in Python 3.8 and greater

Expected Behavior

Stubgen would run successfully.

Actual Behavior

Stubgen produces Critical error during semantic analysis

Your Environment

  • Mypy version used: 0.910
  • MypyStubgen command-line flags: stubgen test.py
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.9.6
  • Operating system and version: Same behavior both on Ubuntu 18.04.5 LTS (WSL2) and with latest python:3-buster Docker images (currently 3.9.6) with mypy 0.910 installed.
@cquick01 cquick01 added the bug mypy got something wrong label Jul 9, 2021
@cquick01
Copy link
Author

cquick01 commented Jul 9, 2021

Looking into it a bit, I think the issue is related to picking a "default" Python 3 version of (3, 6) here

pyversion = defaults.PYTHON2_VERSION if ns.py2 else defaults.PYTHON3_VERSION

This little patch seems to get stubgen working successfully with self documenting f-strings

diff --git a/mypy/stubgen.py b/mypy/stubgen.py
index 91f461b84..920d6405b 100755
--- a/mypy/stubgen.py
+++ b/mypy/stubgen.py
@@ -1623,6 +1623,9 @@ def parse_options(args: List[str]) -> Options:
     if ns.quiet and ns.verbose:
         parser.error('Cannot specify both quiet and verbose messages')

+    if sys.version_info[:2] > pyversion:
+        pyversion = sys.version_info[:2]
+
     # Create the output folder if it doesn't already exist.
     if not os.path.exists(ns.output_dir):
         os.makedirs(ns.output_dir)

But I doubt that's really the right way to fix it... Should defaults.py get updated to point to a newer default python 3 version?

Edit: or maybe add a --python-version argument to stubgen?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-stubgen
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants