-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fail when conditioning on implicit bool #10557
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -84,6 +84,11 @@ | |||||
DESCRIPTOR_SET_NOT_CALLABLE = "{}.__set__ is not callable" # type: Final | ||||||
DESCRIPTOR_GET_NOT_CALLABLE = "{}.__get__ is not callable" # type: Final | ||||||
MODULE_LEVEL_GETATTRIBUTE = '__getattribute__ is not valid at the module level' # type: Final | ||||||
TYPE_CONVERTS_TO_BOOL_IMPLICITLY = \ | ||||||
'"{}" does not implement __bool__ or __len__ so the expression is always truthy' # type: Final | ||||||
ALL_TYPES_IN_UNION_CONVERT_TO_BOOL_IMPLICITLY = \ | ||||||
'Neither of {} implements __bool__ or __len__ '\ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
|
||||||
'so the expression is always truthy' # type: Final | ||||||
|
||||||
# Generic | ||||||
GENERIC_INSTANCE_VAR_CLASS_ACCESS = \ | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
from typing import List, Iterable, Dict, Tuple, Callable, Any, Optional, Iterator | ||
|
||
from mypy import defaults | ||
from mypy import errorcodes as codes | ||
import mypy.api as api | ||
|
||
import pytest | ||
|
@@ -372,16 +373,19 @@ def assert_type(typ: type, value: object) -> None: | |
def parse_options(program_text: str, testcase: DataDrivenTestCase, | ||
incremental_step: int) -> Options: | ||
"""Parse comments like '# flags: --foo' in a test case.""" | ||
options = Options() | ||
flags = re.search('# flags: (.*)$', program_text, flags=re.MULTILINE) | ||
if incremental_step > 1: | ||
flags2 = re.search('# flags{}: (.*)$'.format(incremental_step), program_text, | ||
flags=re.MULTILINE) | ||
if flags2: | ||
flags = flags2 | ||
|
||
disabled_error_codes = {codes.IMPLICIT_BOOL} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Too many fixtures would need multiple changes to add |
||
|
||
if flags: | ||
flag_list = flags.group(1).split() | ||
for code in disabled_error_codes: | ||
flag_list.extend(['--disable-error-code', code.code]) | ||
flag_list.append('--no-site-packages') # the tests shouldn't need an installed Python | ||
targets, options = process_options(flag_list, require_targets=False) | ||
if targets: | ||
|
@@ -393,6 +397,7 @@ def parse_options(program_text: str, testcase: DataDrivenTestCase, | |
# TODO: Enable strict optional in test cases by default (requires *many* test case changes) | ||
options.strict_optional = False | ||
options.error_summary = False | ||
options.disabled_error_codes = disabled_error_codes | ||
|
||
# Allow custom python version to override testcase_pyversion. | ||
if all(flag.split('=')[0] not in ['--python-version', '-2', '--py2'] for flag in flag_list): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from typing import Generic, Sequence, TypeVar | ||
|
||
_T = TypeVar('_T') | ||
|
||
|
||
class object: | ||
def __init__(self): pass | ||
|
||
class type: pass | ||
class int: | ||
def __bool__(self) -> bool: pass | ||
class bool(int): pass | ||
class list(Generic[_T], Sequence[_T]): | ||
def __len__(self) -> int: pass | ||
class str: | ||
def __len__(self) -> int: pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure whether "implicit-bool" should be the code, and whether "implicit conversion" captures what's happening here. Perhaps there's some accepted terminology for this in cpython?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Python docs seem to use the term "boolean operations"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"context of Boolean operation" is also used