Skip to content

Commit 92b8f1c

Browse files
jstasiakilevkivskyi
authored andcommitted
Fix "Could not find builtin symbol 'NoneType'" in the ctypes plugin (#6097)
The error was triggered by creating an array of pointers. Instead, `NoneTyp()` should be used.
1 parent bb0017d commit 92b8f1c

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

mypy/plugins/ctypes.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import mypy.plugin
77
from mypy.maptype import map_instance_to_supertype
88
from mypy.subtypes import is_subtype
9-
from mypy.types import AnyType, CallableType, Instance, Type, TypeOfAny, UnionType, union_items
9+
from mypy.types import (
10+
AnyType, CallableType, Instance, NoneTyp, Type, TypeOfAny, UnionType, union_items
11+
)
1012

1113

1214
def _get_bytes_type(api: 'mypy.plugin.CheckerPluginInterface') -> Instance:
@@ -70,7 +72,7 @@ def _autoconvertible_to_cdata(tp: Type, api: 'mypy.plugin.CheckerPluginInterface
7072
# Pointer-like _SimpleCData subclasses can also be converted from
7173
# an int or None.
7274
allowed_types.append(api.named_generic_type('builtins.int', []))
73-
allowed_types.append(api.named_generic_type('builtins.NoneType', []))
75+
allowed_types.append(NoneTyp())
7476

7577
return UnionType.make_simplified_union(allowed_types)
7678

test-data/unit/check-ctypes.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ reveal_type(ca.value) # E: Revealed type is 'builtins.bytes'
8080
reveal_type(ca.raw) # E: Revealed type is 'builtins.bytes'
8181
[builtins fixtures/floatdict.pyi]
8282

83+
[case testCtypesCharPArrayDoesNotCrash]
84+
import ctypes
85+
86+
# The following line used to crash with "Could not find builtin symbol 'NoneType'"
87+
ca = (ctypes.c_char_p * 0)()
88+
[builtins fixtures/floatdict.pyi]
89+
8390
[case testCtypesCharArrayAttrsPy2]
8491
# flags: --py2
8592
import ctypes

0 commit comments

Comments
 (0)