Skip to content

Commit 100f978

Browse files
JukkaLgvanrossum
authored andcommitted
Don't pass None as the full name to a plugin (#3582)
We would sometimes pass `None` as the full name of a function to a plugin, which could cause a crash within a plugin that doesn't expect it.
1 parent 81ec44b commit 100f978

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

mypy/checkexpr.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ def apply_function_plugin(self,
375375
formal_to_actual: List[List[int]],
376376
args: List[Expression],
377377
num_formals: int,
378-
fullname: Optional[str],
378+
fullname: str,
379379
object_type: Optional[Type],
380380
context: Context) -> Type:
381381
"""Use special case logic to infer the return type of a specific named function/method.
@@ -529,8 +529,10 @@ def check_call(self, callee: Type, args: List[Expression],
529529
# Store the inferred callable type.
530530
self.chk.store_type(callable_node, callee)
531531

532-
if ((object_type is None and self.plugin.get_function_hook(callable_name))
533-
or (object_type is not None and self.plugin.get_method_hook(callable_name))):
532+
if (callable_name
533+
and ((object_type is None and self.plugin.get_function_hook(callable_name))
534+
or (object_type is not None
535+
and self.plugin.get_method_hook(callable_name)))):
534536
ret_type = self.apply_function_plugin(
535537
arg_types, callee.ret_type, arg_kinds, formal_to_actual,
536538
args, len(callee.arg_types), callable_name, object_type, context)

test-data/unit/check-custom-plugin.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ reveal_type(f()) # E: Revealed type is 'builtins.int'
1111
[[mypy]
1212
plugins=<ROOT>/test-data/unit/plugins/fnplugin.py
1313

14+
[case testFunctionPluginFullnameIsNotNone]
15+
# flags: --config-file tmp/mypy.ini
16+
from typing import Callable, TypeVar
17+
f: Callable[[], None]
18+
T = TypeVar('T')
19+
def g(x: T) -> T: return x # This strips out the name of a callable
20+
g(f)()
21+
[file mypy.ini]
22+
[[mypy]
23+
plugins=<ROOT>/test-data/unit/plugins/fnplugin.py
24+
1425
[case testTwoPlugins]
1526
# flags: --config-file tmp/mypy.ini
1627
def f(): ...

test-data/unit/plugins/fnplugin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class MyPlugin(Plugin):
44
def get_function_hook(self, fullname):
55
if fullname == '__main__.f':
66
return my_hook
7+
assert fullname is not None
78
return None
89

910
def my_hook(ctx):

0 commit comments

Comments
 (0)