mypy
mypy copied to clipboard
stubgen: Preserve simple defaults in function signatures
Fixes #13238
See also https://github.com/python/typeshed/issues/8988
cc @AlexWaygood @JelleZijlstra
Some questions for Alex and Jelle:
- Do you want full parity with stubdefaulter?
- ~Should I implement built in container types?~ (added)
- What about name expression default like
def f(x=dict)? - I left a todo in the code about what should be the length limit for a "simple default", currently it is 200 characters. What do you think.
I installed the following packages
matplotlib==3.7.1
numpy==1.24.3
pandas==2.0.2
scipy==1.10.1
seaborn==0.12.2
and generated their stubs with mypy master then added defaults with stubdefaulter then compared the result with defaults added by this PR. The only differences I saw were related to the runtime vs static nature of the two tools:
- stubdefaulter not being able to add defaults to a file due to runtime errors, stubgen defaults were added
- if a default is set to a module variable, stubdefaulter uses its value as the default while stubgen ignores it
Please let me know if you have other ideas to test this change.
@CodiumAI-Agent please review
PR Analysis
- π― Main theme: Preserving simple defaults in function signatures
- π Description and title: Yes
- π Type of PR: Bug fix
- π§ͺ Relevant tests added: Yes
- β¨ Minimal and focused: Yes, the PR is focused on fixing the issue of preserving simple defaults in function signatures.
- π Security concerns: No, the changes made in this PR do not introduce any security concerns.
PR Feedback
-
π‘ General PR suggestions: The PR is well-structured and the changes are focused on the issue at hand. The added function
get_str_default_of_nodeis a good addition and helps in preserving the simple defaults in function signatures. The changes are reflected in the tests as well, which is a good practice. -
π€ Code suggestions:
-
relevant file: mypy/stubgen.py suggestion content: Consider adding a docstring to the
get_str_default_of_nodefunction to explain its purpose and the parameters it takes. This would improve the readability and maintainability of the code. [medium] -
relevant file: mypy/stubgen.py suggestion content: In the
get_str_default_of_nodefunction, you have a check for the length of the default value string. It would be good to make this limit a constant at the beginning of the file or function, to make it easily adjustable. [medium] -
relevant file: mypy/stubgen.py suggestion content: In the
get_str_default_of_nodefunction, there are severalifconditions checking the instance ofrvalue. Consider using a dictionary mapping to simplify this structure and make it more maintainable. [medium] -
relevant file: mypy/stubgen.py suggestion content: In the
get_str_default_of_nodefunction, when handlingTupleExpr,ListExpr,SetExpr, andDictExpr, there is repeated code that could be extracted into a helper function to reduce redundancy. [important]
-
How to use
Tag me in a comment '@CodiumAI-Agent' to ask for a new review after you update the PR. You can also tag me and ask any question, for example '@CodiumAI-Agent is the PR ready for merge?'
@coditamar Please stop, or I'll have to report your account
@coditamar Please stop, or I'll have to report your account
OK. I apologize. thank you
Hello! May I ask what's the status of this? π thank you!
Hello! May I ask what's the status of this? π thank you!
I donβt have an answer for you but if you need default values in stub files now I invite you to try https://github.com/JelleZijlstra/stubdefaulter. I use it regularly and it works very well.
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. β
Diff from mypy_primer, showing the effect of this PR on open source code:
discord.py (https://github.com/Rapptz/discord.py): typechecking got 1.07x faster (189.3s -> 177.3s)
(Performance measurements are based on a single noisy sample)