mypy icon indicating copy to clipboard operation
mypy copied to clipboard

stubgen: Preserve simple defaults in function signatures

Open hamdanal opened this issue 2 years ago β€’ 6 comments
trafficstars

Fixes #13238

See also https://github.com/python/typeshed/issues/8988

cc @AlexWaygood @JelleZijlstra

hamdanal avatar Jun 02 '23 19:06 hamdanal

Some questions for Alex and Jelle:

  1. Do you want full parity with stubdefaulter?
  2. ~Should I implement built in container types?~ (added)
  3. What about name expression default like def f(x=dict)?
  4. 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.

hamdanal avatar Jun 02 '23 19:06 hamdanal

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:

  1. stubdefaulter not being able to add defaults to a file due to runtime errors, stubgen defaults were added
  2. 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.

hamdanal avatar Jun 07 '23 14:06 hamdanal

@CodiumAI-Agent please review

coditamar avatar Jul 07 '23 08:07 coditamar

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_node is 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_node function 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_node function, 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_node function, there are several if conditions checking the instance of rvalue. 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_node function, when handling TupleExpr, ListExpr, SetExpr, and DictExpr, 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?'

CodiumAI-Agent avatar Jul 07 '23 08:07 CodiumAI-Agent

@coditamar Please stop, or I'll have to report your account

hauntsaninja avatar Jul 07 '23 08:07 hauntsaninja

@coditamar Please stop, or I'll have to report your account

OK. I apologize. thank you

coditamar avatar Jul 07 '23 08:07 coditamar

Hello! May I ask what's the status of this? πŸ™‚ thank you!

hoechenberger avatar Nov 23 '23 20:11 hoechenberger

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.

hamdanal avatar Nov 26 '23 20:11 hamdanal

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. βœ…

github-actions[bot] avatar Nov 26 '23 22:11 github-actions[bot]

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)

github-actions[bot] avatar Nov 27 '23 01:11 github-actions[bot]