Skip to content

Conversation

@ilya-pevzner
Copy link
Contributor

This small change fixes a crash in the case when a function arg has both a default value and a non-string type annotation.

Here is an example:

def f(i: int = 0): pass

Before the change, it crashed as follows:

 % stubgen --inspect-mode -o . f.py
Traceback (most recent call last):
  File "/Users/iap/PycharmProjects/py10x/.venv/bin/stubgen", line 7, in <module>
    sys.exit(main())
             ^^^^^^
  File "mypy/stubgen.py", line 2047, in main
  File "mypy/stubgen.py", line 1848, in generate_stubs
  File "mypy/stubgen.py", line 1806, in generate_stub_for_py_module
  File "/Users/iap/PycharmProjects/py10x/.venv/lib/python3.11/site-packages/mypy/stubgenc.py", line 452, in generate_module
    self.generate_function_stub(name, obj, output=functions)
  File "/Users/iap/PycharmProjects/py10x/.venv/lib/python3.11/site-packages/mypy/stubgenc.py", line 633, in generate_function_stub
    self.process_inferred_sigs(inferred)
  File "/Users/iap/PycharmProjects/py10x/.venv/lib/python3.11/site-packages/mypy/stubgenc.py", line 602, in process_inferred_sigs
    arg.type = self.strip_or_import(arg.type)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/iap/PycharmProjects/py10x/.venv/lib/python3.11/site-packages/mypy/stubgenc.py", line 398, in strip_or_import
    parsed_type = parse_type_comment(type_name, 0, 0, None)[1]
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "mypy/fastparse.py", line 303, in parse_type_comment
TypeError: str object expected; got type

After the change, it works properly:

% stubgen --inspect-mode -o . f.py                      
Processed 1 modules
Generated ./f.pyi

% cat f.pyi 
def f(i: int = ...): ...

do not crash in the case or when a function arg has both a default value and a non-string type annotation
Copy link
Collaborator

@sterliakov sterliakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_annotations was right there for exactly this purpose (and already used in another branch), and argspec.annotations is definitely not limited to string values. Looks like a simple omission in the original implementation (#15770). Thanks!

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks right!

(re the review request: i only wrote stubtest; i don't know that much about stubgen/c :-) )

@hauntsaninja hauntsaninja merged commit 04a586c into python:master Oct 11, 2025
14 checks passed
@sterliakov
Copy link
Collaborator

I just remember that you sometimes manage to free up a few minutes to go through small and simple PRs that tend to accumulate quickly:)

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.

3 participants