-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance the protocol checker (#3259)
This commit adds multiple checks for various Python protocols E0304 (invalid-bool-returned): __bool__ did not return a bool E0305 (invalid-index-returned): __index__ did not return an integer E0306 (invalid-repr-returned): __repr__ did not return a string E0307 (invalid-str-returned): __str__ did not return a string E0308 (invalid-bytes-returned): __bytes__ did not return a string E0309 (invalid-hash-returned): __hash__ did not return an integer E0310 (invalid-length-hint-returned): __length_hint__ did not return a non-negative integer E0311 (invalid-format-returned): __format__ did not return a string E0312 (invalid-getnewargs-returned): __getnewargs__ did not return a tuple E0313 (invalid-getnewargs-ex-returned): __getnewargs_ex__ did not return a tuple of the form (tuple, dict) Close #560
- Loading branch information
1 parent
01dfa52
commit c632201
Showing
24 changed files
with
942 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -352,3 +352,5 @@ contributors: | |
* Bastien Vallet: contributor | ||
|
||
* Pek Chhan: contributor | ||
|
||
* Craig Henriques: contributor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"""Check invalid value returned by __bool__ """ | ||
|
||
# pylint: disable=too-few-public-methods,missing-docstring,no-self-use,import-error, useless-object-inheritance | ||
import six | ||
|
||
from missing import Missing | ||
|
||
|
||
class FirstGoodBool(object): | ||
"""__bool__ returns <type 'bool'>""" | ||
|
||
def __bool__(self): | ||
return True | ||
|
||
|
||
class SecondGoodBool(object): | ||
"""__bool__ returns <type 'bool'>""" | ||
|
||
def __bool__(self): | ||
return bool(0) | ||
|
||
|
||
class BoolMetaclass(type): | ||
def __bool__(cls): | ||
return True | ||
|
||
|
||
@six.add_metaclass(BoolMetaclass) | ||
class ThirdGoodBool(object): | ||
"""Bool through the metaclass.""" | ||
|
||
|
||
class FirstBadBool(object): | ||
""" __bool__ returns an integer """ | ||
|
||
def __bool__(self): # [invalid-bool-returned] | ||
return 1 | ||
|
||
|
||
class SecondBadBool(object): | ||
""" __bool__ returns str """ | ||
|
||
def __bool__(self): # [invalid-bool-returned] | ||
return "True" | ||
|
||
|
||
class ThirdBadBool(object): | ||
""" __bool__ returns node which does not have 'value' in AST """ | ||
|
||
def __bool__(self): # [invalid-bool-returned] | ||
return lambda: 3 | ||
|
||
|
||
class AmbigousBool(object): | ||
""" Uninferable return value """ | ||
__bool__ = lambda self: Missing | ||
|
||
|
||
class AnotherAmbiguousBool(object): | ||
"""Potential uninferable return value""" | ||
def __bool__(self): | ||
return bool(Missing) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
invalid-bool-returned:36:FirstBadBool.__bool__:__bool__ does not return bool | ||
invalid-bool-returned:43:SecondBadBool.__bool__:__bool__ does not return bool | ||
invalid-bool-returned:50:ThirdBadBool.__bool__:__bool__ does not return bool |
Oops, something went wrong.