This product is not supported for your selected Datadog site. ().
이 페이지는 아직 영어로 제공되지 않습니다. 번역 작업 중입니다. 현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.
Metadata
ID:python-security/no-empty-list-as-parameter
Language: Python
Severity: Warning
Category: Security
Description
Developers should not be setting a default argument to an empty list. Instead, use None and check if the value is defined. Using a default list can cause unwanted behavior as the value of the argument is only evaluated once when the function is defined, not when it is run. Because of this, each function call will reference the same underlying memory when the default value is used, which can lead to unwanted behavior.
defnewFunction(arg1,arg2:int,arg3=[],arg4:MyType=[]):# do not use an empty list as a default parameterarg3.append(arg2)arg4.append(arg1)print(arg3,arg4)newFunction('a',1)newFunction('b',2)newFunction('c',3)# Will print:# [1] ['a']# [1, 2] ['a', 'b']# [1, 2, 3] ['a', 'b', 'c']
Compliant Code Examples
defnewFunction(arg1,arg2:int,arg3=None,arg4=None):# do not use an empty list as a default parameterifarg3isNone:arg3=[]ifarg4isNone:arg4=[]arg3.append(arg2)arg4.append(arg1)print(arg3,arg4)newFunction('a',1)newFunction('b',2)newFunction('c',3)# Will print:# [1] ['a']# [2] ['b']# [3] ['c']
원활한 통합. Datadog Code Security를 경험해 보세요
Datadog Code Security
이 규칙을 사용해 Datadog Code Security로 코드를 분석하세요
규칙 사용 방법
1
2
rulesets:- python-security # Rules to enforce Python security.
리포지토리 루트에 위의 내용을 포함하는 static-analysis.datadog.yml을 만듭니다
무료 IDE 플러그인을 사용하거나 CI 파이프라인에 Code Security 검사를 추가합니다