Originally reported by: Antony Lee (BitBucket: anntzer, GitHub: @anntzer?)
Linting
#!python
from pathlib import Path
Path("foo").with_name("bar").open("rb")
gives
E: 2,29: Instance of 'PurePath' has no 'open' member (no-member)
but with_name actually returns a Path here (more generally, an object with the same class as the initial object), not a PurePath, so the inference and the error message are wrong.
_Original comment by_ Claudiu Popa (BitBucket: PCManticore, GitHub: @PCManticore):
I'll have a look soon.
Will it be fixed any time soon ? Thanks.
Probably not as soon as I was expecting it to be. Most probably it requires a special astroid brain tip for Path objects.
Minimal astroid examples that expose a bug:
def test1(self):
ast = extract_node("""
class C:
@classmethod
def factory(cls):
return cls()
def m(self):
return self.__class__.factory()
class D(C):
pass
C().m() #@
D().m() #@
""", 'm')
should_be_C = list(ast[0].infer())
should_be_D = list(ast[1].infer())
self.assertEqual(1, len(should_be_C))
self.assertEqual(1, len(should_be_D))
self.assertEqual('m.C', should_be_C[0].qname())
self.assertEqual('m.D', should_be_D[0].qname())
def test2(self):
ast = extract_node("""
class C:
@classmethod
def factory(cls):
return object.__new__(cls)
def m(self):
return self.__class__.factory()
class D(C):
pass
C().m() #@
D().m() #@
""", 'm')
should_be_C = list(ast[0].infer())
should_be_D = list(ast[1].infer())
self.assertEqual(1, len(should_be_C))
self.assertEqual(1, len(should_be_D))
self.assertEqual('m.C', should_be_C[0].qname())
self.assertEqual('m.D', should_be_D[0].qname())
test1 passes, test2 fails. The only difference is a way how new instance of object is created in factory.
It seems like dealing with a new callcontext results in lack of data needed to infer these types properly. I don't know how to fix it (yet), so I'll just leave it there, maybe someone can figure this out.
See pathlib.py source for reference.
Any update on this? I fail the tests on Pylint issues and the need to disable the error on every use of .resolve() is quite annoying.
No one is currently working on this. We have lots of issues in our backlog and we're all volunteers :-) We'll get to it at some point.
@PCManticore Of course. And your sacrifices are much appreciated :) I didn't want to sound like giving orders or requesting you to fix it right now. I was just pointing out that this is still a problem, and these little things can drive people away from Pylint. But I myself am not switching to flake8 :)
Most helpful comment
@PCManticore Of course. And your sacrifices are much appreciated :) I didn't want to sound like giving orders or requesting you to fix it right now. I was just pointing out that this is still a problem, and these little things can drive people away from Pylint. But I myself am not switching to flake8 :)