When I run the following block of code with pylint, I get no errors.
import json
for key, value in json.loads('{"foo":"bar"}').items():
print(key, value)
Upon switching out json
with simplejson
and then running pylint, I get the error:
Instance of 'bool' has no 'items' member (but some types could not be inferred) (maybe-no-member)
From comparing the infered result of astroid
for both simplejson.loads & json.loads - it appears that even when both json & simplejson are compiled with the c_speedups, astroid picks up the python version of the scan_once function when dealing with the simplejson
library, and the c version when dealing with the json
library.
import astroid.builder
builder = astroid.builder.AstroidBuilder()
ast = builder.string_build("""
import simplejson
import json
x = json.loads('"test"')
y = simplejson.loads('"test"')
""")
json_assignment, simplejson_assignment = list(ast.get_children())[2:]
print "json:", list(json_assignment.get_children())[-1].infered()
print "simplejson:", list(simplejson_assignment.get_children())[-1].infered()
Running the above code outputs:
json: [YES]
simplejson: [YES, <Const(NoneType) l.97 [simplejson.scanner] at Ox102720290>, <Const(bool) l.99 [simplejson.scanner] at Ox1027207d0>, <Const(bool) l.101 [simplejson.scanner] at Ox102720d10>]
I'm not sure why astroid
has different behavior when inferring the return types of simplejson.loads
& json.loads
- but the above implies that the scenario with the json
library might be getting past pylint accidentally.
Pylint
can't know that the result type of json.loads
will be dict
, because it depends on the input string. It is typical that a dynamical code like getattr, setattr etc. can not be introspected deep enough in a predictable short time and therefore is guessed, not introspected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With