79 lines
1.6 KiB
Plaintext
79 lines
1.6 KiB
Plaintext
== Why is this an issue?
|
|
|
|
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.
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,python]
|
|
----
|
|
######################
|
|
# 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)
|
|
|
|
include::message.adoc[]
|
|
|
|
include::highlighting.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::../comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|