Consider the following use case (minimum example):
def get_func(param):
if param is None:
def func():
return None
else:
def func():
return param
return func
def process_val(param):
func = get_func(param)
val = func()
# Do stuff with 'val'; *None* is an useful case.
return val
Here, func() can return None or not, depending on the value of param, but Pylint triggers E1128 for this, with the following explanation:
Used when an assignment is done on a function call but the inferred function returns nothing but None.
I am tempted to just disable the warning for this code, but it is actually classified as an Error, which makes me think this has actually produced bugs in the past, so I would like to understand: is this a Pylint error, that doesn't see that sometimes the function created will return something else than None? Or is it considered too bad practice to possibly have a function that always returns None? Maybe some other explanation that I cannot see?
In case this seems like a too convoluted, the actual use case is more like this:
def get_func(source):
if source is None:
def func():
return None
elif source is "webcam":
# Open webcam...
def func():
# Capture frame from webcam
return frame
elif source is "server":
# Open connection to server...
def func():
# Read data from server.
return data
# Other cases...
return func
def process_val(source):
data_func = get_func(source)
# Here, do stuff in a loop, or pass *data_func* to other functions...
# The code that uses the *data_func* knows that *None* means that
# data could not be read and that's OK.
For the code that uses data_func, it's simpler like this than to having to consider the value of source to decide if the data will always be None. To me this seems a valid functional-style approach (maybe I'm wrong and this is not the Pythonic way).
(I'm using Pylint 2.12.2)
If the function does not always return None, then it's a false positive from pylint not understanding your code well. If the function always return None you have no reason to assign it in a variable, and it means the code is at best is doing a useless assignment, not doing what you think it does, or at worst completely wrong. Not sure why it's an error message and not a warning though.
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