Skip to content
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

[too-many-positional-arguments] Better documentation following questions #10049

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/data/messages/t/too-many-positional-arguments/bad.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# +1: [too-many-positional-arguments]
def calculate_drag_force(velocity, area, density, drag_coefficient):
"""Each argument is positional-or-keyword unless a `/` or `*` is present."""
return 0.5 * drag_coefficient * density * area * velocity**2


Expand Down
17 changes: 9 additions & 8 deletions doc/data/messages/t/too-many-positional-arguments/details.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Positional arguments work well for cases where the the use cases are
self-evident, such as unittest's ``assertEqual(first, second, msg=None)``.
Comprehensibility suffers beyond a handful of arguments, though, so for
functions that take more inputs, require that additional arguments be
passed by *keyword only* by preceding them with ``*``:
Good function signatures don’t have many positional parameters. For almost all
interfaces, comprehensibility suffers beyond a handful of arguments.

.. code-block:: python
Positional arguments work well for cases where the the use cases are
self-evident, such as unittest's ``assertEqual(first, second, "assert msg")``
or ``zip(fruits, vegetables)``.

def make_noise(self, volume, *, color=noise.PINK, debug=True):
...
There are a few exceptions where four or more positional parameters make sense,
for example ``rgba(1.0, 0.5, 0.3, 1.0)``, because it uses a very well-known and
well-established convention, and using keywords all the time would be a waste
of time.
7 changes: 7 additions & 0 deletions doc/data/messages/t/too-many-positional-arguments/good.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
def calculate_drag_force(*, velocity, area, density, drag_coefficient):
"""This function is 'Keyword only' for all args due to the '*'."""
return 0.5 * drag_coefficient * density * area * velocity**2


# This is now impossible to do and will raise a TypeError:
# drag_force = calculate_drag_force(30, 2.5, 1.225, 0.47)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# TypeError: calculate_drag_force() takes 0 positional arguments but 4 were given

# And this is the only way to call 'calculate_drag_force'
drag_force = calculate_drag_force(
velocity=30, area=2.5, density=1.225, drag_coefficient=0.47
)
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
- `Ruff discussion <https://github.com/astral-sh/ruff/issues/8946>`_
- `Pylint issue <https://github.com/pylint-dev/pylint/issues/9099>`_
- `Special parameters in python <https://docs.python.org/3/tutorial/controlflow.html#special-parameters>`_
Loading