Skip to content

Commit 4ddd43e

Browse files
Pierre-Sassoulasflying-sheepjacobtylerwalls
authored
[too-many-positional-arguments] Better documentation following questions (#10049)
* [too-many-positional-arguments] Better documentation following questions See https://github.com/astral-sh/ruff/issues/8946\#issuecomment-2437547742 Co-authored-by: Philipp Albrecht <[email protected]> * Apply suggestions from code review Co-authored-by: Jacob Walls <[email protected]> --------- Co-authored-by: Philipp Albrecht <[email protected]> Co-authored-by: Jacob Walls <[email protected]>
1 parent 71e2d0f commit 4ddd43e

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

doc/data/messages/t/too-many-positional-arguments/bad.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# +1: [too-many-positional-arguments]
22
def calculate_drag_force(velocity, area, density, drag_coefficient):
3+
"""Each argument is positional-or-keyword unless a `/` or `*` is present."""
34
return 0.5 * drag_coefficient * density * area * velocity**2
45

56

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
Positional arguments work well for cases where the the use cases are
2-
self-evident, such as unittest's ``assertEqual(first, second, msg=None)``.
3-
Comprehensibility suffers beyond a handful of arguments, though, so for
4-
functions that take more inputs, require that additional arguments be
5-
passed by *keyword only* by preceding them with ``*``:
1+
Good function signatures don’t have many positional parameters. For almost all
2+
interfaces, comprehensibility suffers beyond a handful of arguments.
63

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

9-
def make_noise(self, volume, *, color=noise.PINK, debug=True):
10-
...
8+
There are a few exceptions where four or more positional parameters make sense,
9+
for example ``rgba(1.0, 0.5, 0.3, 1.0)``, because it uses a very well-known and
10+
well-established convention, and using keywords all the time would be a waste
11+
of time.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
def calculate_drag_force(*, velocity, area, density, drag_coefficient):
2+
"""This function is 'Keyword only' for all args due to the '*'."""
23
return 0.5 * drag_coefficient * density * area * velocity**2
34

45

6+
# This is now impossible to do and will raise a TypeError:
7+
# drag_force = calculate_drag_force(30, 2.5, 1.225, 0.47)
8+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9+
# TypeError: calculate_drag_force() takes 0 positional arguments but 4 were given
10+
11+
# And this is the only way to call 'calculate_drag_force'
512
drag_force = calculate_drag_force(
613
velocity=30, area=2.5, density=1.225, drag_coefficient=0.47
714
)
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
- `Ruff discussion <https://github.com/astral-sh/ruff/issues/8946>`_
2-
- `Pylint issue <https://github.com/pylint-dev/pylint/issues/9099>`_
1+
- `Special parameters in python <https://docs.python.org/3/tutorial/controlflow.html#special-parameters>`_

0 commit comments

Comments
 (0)