Repository: python/mypy

Issue:
stubgen reports wrong Python version when used with debug f-string
<!--
  If you're new to mypy and you're not sure whether what you're experiencing is a mypy bug, please see the "Question and Help" form
  instead.
-->

**Bug Report**

When running stubgen on a module that contains a debug f-string, stubgen reports a "Critical error" and reports the wrong python version.

**To Reproduce**

```
# mod.py
def fn() -> int:
    i = 5
    print(f'{i=}')
    return 0

C:\> stubgen mod.py
Critical error during semantic analysis: mod.py:3: error: f-string: self documenting expressions are only supported in Python 3.8 and greater
```

**Expected Behavior**
```
C:\> stubgen mod.py
Processed 1 modules
Generated out\mod.pyi
```

**Actual Behavior**
```
C:\> stubgen mod.py
Critical error during semantic analysis: mod.py:3: error: f-string: self documenting expressions are only supported in Python 3.8 and greater
```

**Your Environment**

- Mypy version used: 0.910
- Mypy command-line flags: none
- Mypy configuration options from `mypy.ini` (and other config files): None
- Python version used: 3.9.6 (no other Pythons installed)
- Operating system and version: Win 10.0.19043



Stubgen giving error about self documenting expressions in f-strings
**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](https://mypy-lang.blogspot.com/2021/01/mypy-0800-released.html) 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
- ~~Mypy~~Stubgen 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.


Discussion:
Looks like stubgen effectively hardcodes 3.6. Easy enough to fix...
Looking into it a bit, I think the issue is related to picking a "default" Python 3 version of `(3, 6)` [here](https://github.com/python/mypy/blob/30efa34961e44675ccc405cb36b31745ef309660/mypy/stubgen.py#L1618)

```python
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
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?

Resolving patch:
diff --git a/mypy/stubgen.py b/mypy/stubgen.py
--- a/mypy/stubgen.py
+++ b/mypy/stubgen.py
@@ -1646,7 +1646,7 @@ def parse_options(args: List[str]) -> Options:
 
     ns = parser.parse_args(args)
 
-    pyversion = defaults.PYTHON2_VERSION if ns.py2 else defaults.PYTHON3_VERSION
+    pyversion = defaults.PYTHON2_VERSION if ns.py2 else sys.version_info[:2]
     if not ns.interpreter:
         ns.interpreter = sys.executable if pyversion[0] == 3 else default_py2_interpreter()
     if ns.modules + ns.packages and ns.files:


Test patch: