Reputation: 13
I write in PyCharm and get a bug (in line "func(dict_data)"): Unexpected argument(s) Possible callees: A.foo(dict_data: dict) A.bar(dict_data: dict).
Is it a PyCharm bug or am I doing something wrong?
PyCharm 2020.3
class A:
def __init__(self):
self.functions = {
"foo": self.foo,
"bar": self.bar
}
def typing(self, dict_data: dict):
for key, value in dict_data.items():
if key in self.functions:
func = self.functions[value["type"]]
func(dict_data)
def foo(self, dict_data: dict):
print(dict_data)
def bar(self, dict_data: dict):
print(dict_data)
class B:
def __init__(self):
self.data = {
"foo": {"type": "foo"},
"bar": {"type": "bar"}
}
def get(self) -> dict:
return self.data
if __name__ == "__main__":
b = B()
data = b.get()
a = A()
a.typing(data)
Upvotes: 0
Views: 5059
Reputation: 1
you can get the warning away by adding lambdas:
self.functions = {
"foo": lambda x: self.foo(x),
"bar": lambda x: self.bar(x)
}
Upvotes: 0
Reputation: 6017
A better approach is to add a type hint to func
like
func: Callable[[dict], None] = self.functions[value["type"]]
(Don't forget to add from typing import Callable
)
This will get rid of the IDE warning by giving it additional information to correctly determine the types. Basically, it will no longer see func
as a Union
type but directly know that it's a Callable
a known method signature.
Upvotes: 2
Reputation: 33
you can get the warning away by adding lambdas:
self.functions = {
"foo": lambda x: self.foo,
"bar": lambda x: self.bar
}
Upvotes: 0
Reputation: 831
func is actually a variable in your code, but you are calling it as a function which does not exist there, so PyCharm is guessing what function you might want to use instead which can accommodate dict_data as it's argument.
This 'def function_name(arguments : datatype)' in the pic is how function is identified but your ss has the keyword 'Union' and therefore func's datatype is Union.
Upvotes: 1