-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
False-positive E1101 (no-member) for writable file-like object #3234
Comments
Can confirm, just ran into this as well in our case we're using
And we get this linting error
I'm not even sure how it gets to the |
Thanks for the example, folks, I can confirm the bug. |
i wonder, if it's the same issue or not: from typing import Any, List, Optional
NOT_FOUND_ATOM = object()
class AnnotatedDataType():
def _key_not_exists(self, key):
return getattr(self, key, NOT_FOUND_ATOM) is NOT_FOUND_ATOM
def __init__(self, **kwargs) -> None:
for key, value in kwargs.items():
setattr(self, key, value)
for key in self.__annotations__: # pylint: disable=no-member
if self._key_not_exists(key):
raise TypeError(
f"'{self.__class__.__name__}' does "
f"not have required attribute '{key}' set"
)
def __setattr__(self, key: str, value: Any) -> None:
if (
not getattr(self, "__annotations__", None) or
self.__annotations__.get(key, NOT_FOUND_ATOM) is NOT_FOUND_ATOM # pylint: disable=no-member
) and self._key_not_exists(key):
raise TypeError(
f"'{self.__class__.__name__}' does "
f"not have attribute '{key}'"
)
super().__setattr__(key, value)
class PackagesNotFound(AnnotatedDataType, Exception):
packages: List[str]
wanted_by: Optional[List[str]] = None
def __init__(self, packages: List[str], wanted_by: Optional[List[str]] = None) -> None:
AnnotatedDataType.__init__(self, packages=packages, wanted_by=wanted_by)
message = ', '.join(packages)
if wanted_by:
message += f" wanted by {', '.join(wanted_by)}"
Exception.__init__(self, message)
class PackagesNotFoundInRepo(PackagesNotFound):
pass
def main():
try:
raise PackagesNotFoundInRepo(packages=['a', 'b', 'c'])
except PackagesNotFound as exc:
print(exc.packages)
# ^ here pylint is NOT complaining
try:
raise PackagesNotFoundInRepo(packages=['a', 'b', 'c'])
except PackagesNotFoundInRepo as exc:
print(exc.packages)
# ^ [pylint] no-member: Instance of 'PackagesNotFoundInRepo' has no 'packages' member
if __name__ == "__main__":
main() |
It looks like this is due to pylint-dev/pylint#3234
I think there is the same issue with from typing import TypedDict
class Example(TypedDict):
string: str
integer: int
boolean: bool
print(Example.__annotations__) Outputs: But pylint complains: |
i slightly updated my previous message, because the main point was not the annotations (which also indeed are not working there) |
As for from typing import NamedTuple
class Foo(NamedTuple):
foobar: str
print(Foo.__annotations__)
$ pylint --version
pylint 2.10.2
astroid 2.7.2
Python 3.9.6 (default, Aug 23 2021, 11:20:01) |
This issues seems to be fixed when testing with the latest pylint version. |
Latest tagged version? With
for the example I posted above problem still persists. |
@fenuks I'd tested it with Python 3.10 which works. I can't reproduce the original issue with |
Just like #3233, the original issue (not sure exactly where python3.6 -m venv venv
venv/bin/python -m pip install pylint==2.4.4
venv/bin/pylint sscce.py # repro
venv/bin/python -m pip install astroid~=2.4.0
venv/bin/pylint sscce.py # NO repro |
Closing as the main issue was fixed, and I opened #7126 for the one that appeared along the way. |
Steps to reproduce
Note: Like #3233, this was found while using
paramiko
:local_file
is aparamiko.SFTPFile
which inherits fromparamiko.BufferedFile
which inheritsparamiko.util.ClosingContextManager
.Current behavior
Expected behavior
(no error)
pylint --version output
The text was updated successfully, but these errors were encountered: