rspec/rules/S930/python/rule.adoc

95 lines
2.4 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-01-27 13:42:22 +01:00
Calling a function or a method with fewer or more arguments than expected will raise a ``++TypeError++``. This is usually a bug and should be fixed.
2020-06-30 12:50:59 +02:00
=== Noncompliant code example
2020-06-30 12:50:59 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2020-06-30 12:50:59 +02:00
----
######################
# Positional Arguments
######################
param_args = [1, 2, 3]
param_kwargs = {'x': 1, 'y': 2}
def func(a, b=1):
print(a, b)
def positional_unlimited(a, b=1, *args):
print(a, b, *args)
func(1)
func(1, 42)
func(1, 2, 3) # Noncompliant. Too many positional arguments
func() # Noncompliant. Missing positional argument for "a"
positional_unlimited(1, 2, 3, 4, 5)
def positional_limited(a, *, b=2):
print(a, b)
positional_limited(1, 2) # Noncompliant. Too many positional arguments
#############################
# Unexpected Keyword argument
#############################
def keywords(a=1, b=2, *, c=3):
print(a, b, c)
keywords(1)
keywords(1, z=42) # Noncompliant. Unexpected keyword argument "z"
def keywords_unlimited(a=1, b=2, *, c=3, **kwargs):
print(a, b, kwargs)
keywords_unlimited(a=1, b=2, z=42)
#################################
# Mandatory Keyword argument only
#################################
def mandatory_keyword(a, *, b):
print(a, b)
mandatory_keyword(1, b=2)
mandatory_keyword(1) # Noncompliant. Missing keyword argument "b"
----
include::../see.adoc[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* When there is one or more unexpected positional argument: "Remove XXX unexpected argument; 'foo' expects YYY positional arguments."
* When there is one or more unexpected named argument: "Remove this unexpected named argument X"
* When the maximum number of positional arguments is reached and {empty}*args is given: "Remove "*XXX"; maximum number of positional arguments already reached."
* When the maximum number of keyword arguments is reached and {empty}*{empty}*kwargs is given: "Remove "**XXX"; maximum number of keyword arguments already reached."
* When there are missing positional arguments: "Add XXX missing arguments; 'foo' expects YYY positional arguments."
* When there are missing keyword arguments: "Add the missing keyword argument XXX"
=== Highlighting
Primary: The function name
Secondary:
* The function definition
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]