forked from pylint-dev/pylint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Omit no-member error if guarded behind if stmt (pylint-dev#4764)
* Omit no-member error if guarded behind if stmt Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
- Loading branch information
1 parent
ae6cbd1
commit 107f59e
Showing
5 changed files
with
121 additions
and
0 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
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,79 @@ | ||
# pylint: disable=invalid-name,missing-docstring,pointless-statement | ||
from datetime import datetime | ||
from typing import Union | ||
|
||
value = "Hello World" | ||
value.isoformat() # [no-member] | ||
|
||
|
||
if isinstance(value, datetime): | ||
value.isoformat() | ||
else: | ||
value.isoformat() # [no-member] | ||
|
||
|
||
def func(): | ||
if hasattr(value, "load"): | ||
value.load() | ||
|
||
if getattr(value, "load", None): | ||
value.load | ||
|
||
|
||
if value.none_existent(): # [no-member] | ||
pass | ||
|
||
res = value.isoformat() if isinstance(value, datetime) else value | ||
|
||
|
||
class Base: | ||
_attr_state: Union[str, datetime] = "Unknown" | ||
|
||
@property | ||
def state(self) -> Union[str, datetime]: | ||
return self._attr_state | ||
|
||
def some_function(self) -> str: | ||
state = self.state | ||
if isinstance(state, datetime): | ||
return state.isoformat() | ||
return str(state) | ||
|
||
|
||
# https://github.com/PyCQA/pylint/issues/1990 | ||
# Attribute access after 'isinstance' should not cause 'no-member' error | ||
import subprocess # pylint: disable=wrong-import-position # noqa: E402 | ||
|
||
try: | ||
subprocess.check_call(['ls', '-']) # Deliberately made error in this line | ||
except Exception as err: | ||
if isinstance(err, subprocess.CalledProcessError): | ||
print(f'Subprocess error occured. Return code: {err.returncode}') | ||
else: | ||
print(f'An error occured: {str(err)}') | ||
raise | ||
|
||
|
||
# https://github.com/PyCQA/pylint/issues/4168 | ||
# 'encode' for 'arg' should not cause 'no-member' error | ||
mixed_tuple = (b"a", b"b", b"c", b"d") | ||
byte_tuple = [arg.encode('utf8') if isinstance(arg, str) else arg for arg in mixed_tuple] | ||
|
||
for arg in mixed_tuple: | ||
if isinstance(arg, str): | ||
print(arg.encode('utf8')) | ||
else: | ||
print(arg) | ||
|
||
|
||
# https://github.com/PyCQA/pylint/issues/1162 | ||
# Attribute access after 'isinstance' should not cause 'no-member' error | ||
class FoobarException(Exception): | ||
foobar = None | ||
|
||
try: # noqa: E305 | ||
pass | ||
except Exception as ex: | ||
if isinstance(ex, FoobarException): | ||
ex.foobar | ||
raise |
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 @@ | ||
no-member:6:0::Instance of 'str' has no 'isoformat' member:INFERENCE | ||
no-member:12:4::Instance of 'str' has no 'isoformat' member:INFERENCE | ||
no-member:23:3::Instance of 'str' has no 'none_existent' member:INFERENCE |