Skip to content

Commit 17b4693

Browse files
AlexWaygoodJelleZijlstra
authored andcommitted
Fix ctypes plugin (hopefully)
1 parent b1761f4 commit 17b4693

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

mypy/plugins/ctypes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ def _find_simplecdata_base_arg(
3030
3131
None is returned if _SimpleCData appears nowhere in tp's (direct or indirect) bases.
3232
"""
33-
if tp.type.has_base("ctypes._SimpleCData"):
33+
if tp.type.has_base("_ctypes._SimpleCData"):
3434
simplecdata_base = map_instance_to_supertype(
3535
tp,
36-
api.named_generic_type("ctypes._SimpleCData", [AnyType(TypeOfAny.special_form)]).type,
36+
api.named_generic_type("_ctypes._SimpleCData", [AnyType(TypeOfAny.special_form)]).type,
3737
)
3838
assert len(simplecdata_base.args) == 1, "_SimpleCData takes exactly one type argument"
3939
return get_proper_type(simplecdata_base.args[0])
@@ -88,7 +88,7 @@ def _autounboxed_cdata(tp: Type) -> ProperType:
8888
return make_simplified_union([_autounboxed_cdata(t) for t in tp.items])
8989
elif isinstance(tp, Instance):
9090
for base in tp.type.bases:
91-
if base.type.fullname == "ctypes._SimpleCData":
91+
if base.type.fullname == "_ctypes._SimpleCData":
9292
# If tp has _SimpleCData as a direct base class,
9393
# the auto-unboxed type is the single type argument of the _SimpleCData type.
9494
assert len(base.args) == 1
@@ -102,7 +102,7 @@ def _get_array_element_type(tp: Type) -> ProperType | None:
102102
"""Get the element type of the Array type tp, or None if not specified."""
103103
tp = get_proper_type(tp)
104104
if isinstance(tp, Instance):
105-
assert tp.type.fullname == "ctypes.Array"
105+
assert tp.type.fullname == "_ctypes.Array"
106106
if len(tp.args) == 1:
107107
return get_proper_type(tp.args[0])
108108
return None

mypy/plugins/default.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class DefaultPlugin(Plugin):
4141
def get_function_hook(self, fullname: str) -> Callable[[FunctionContext], Type] | None:
4242
from mypy.plugins import ctypes, singledispatch
4343

44-
if fullname == "ctypes.Array":
44+
if fullname == "_ctypes.Array":
4545
return ctypes.array_constructor_callback
4646
elif fullname == "functools.singledispatch":
4747
return singledispatch.create_singledispatch_function_callback
@@ -69,7 +69,7 @@ def get_method_signature_hook(
6969
return typed_dict_pop_signature_callback
7070
elif fullname in {n + ".update" for n in TPDICT_FB_NAMES}:
7171
return typed_dict_update_signature_callback
72-
elif fullname == "ctypes.Array.__setitem__":
72+
elif fullname == "_ctypes.Array.__setitem__":
7373
return ctypes.array_setitem_callback
7474
elif fullname == singledispatch.SINGLEDISPATCH_CALLABLE_CALL_METHOD:
7575
return singledispatch.call_singledispatch_function_callback
@@ -92,9 +92,9 @@ def get_method_hook(self, fullname: str) -> Callable[[MethodContext], Type] | No
9292
return typed_dict_pop_callback
9393
elif fullname in {n + ".__delitem__" for n in TPDICT_FB_NAMES}:
9494
return typed_dict_delitem_callback
95-
elif fullname == "ctypes.Array.__getitem__":
95+
elif fullname == "_ctypes.Array.__getitem__":
9696
return ctypes.array_getitem_callback
97-
elif fullname == "ctypes.Array.__iter__":
97+
elif fullname == "_ctypes.Array.__iter__":
9898
return ctypes.array_iter_callback
9999
elif fullname == singledispatch.SINGLEDISPATCH_REGISTER_METHOD:
100100
return singledispatch.singledispatch_register_callback
@@ -105,9 +105,9 @@ def get_method_hook(self, fullname: str) -> Callable[[MethodContext], Type] | No
105105
def get_attribute_hook(self, fullname: str) -> Callable[[AttributeContext], Type] | None:
106106
from mypy.plugins import ctypes, enums
107107

108-
if fullname == "ctypes.Array.value":
108+
if fullname == "_ctypes.Array.value":
109109
return ctypes.array_value_callback
110-
elif fullname == "ctypes.Array.raw":
110+
elif fullname == "_ctypes.Array.raw":
111111
return ctypes.array_raw_callback
112112
elif fullname in enums.ENUM_NAME_ACCESS:
113113
return enums.enum_name_callback

test-data/unit/check-ctypes.test

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class MyCInt(ctypes.c_int):
77
intarr4 = ctypes.c_int * 4
88
a = intarr4(1, ctypes.c_int(2), MyCInt(3), 4)
99
intarr4(1, 2, 3, "invalid") # E: Array constructor argument 4 of type "str" is not convertible to the array element type "c_int"
10-
reveal_type(a) # N: Revealed type is "ctypes.Array[ctypes.c_int]"
10+
reveal_type(a) # N: Revealed type is "_ctypes.Array[ctypes.c_int]"
1111
reveal_type(a[0]) # N: Revealed type is "builtins.int"
1212
reveal_type(a[1:3]) # N: Revealed type is "builtins.list[builtins.int]"
1313
a[0] = 42
@@ -33,7 +33,7 @@ myintarr4 = MyCInt * 4
3333
mya = myintarr4(1, 2, MyCInt(3), 4)
3434
myintarr4(1, ctypes.c_int(2), MyCInt(3), "invalid") # E: Array constructor argument 2 of type "c_int" is not convertible to the array element type "MyCInt" \
3535
# E: Array constructor argument 4 of type "str" is not convertible to the array element type "MyCInt"
36-
reveal_type(mya) # N: Revealed type is "ctypes.Array[__main__.MyCInt]"
36+
reveal_type(mya) # N: Revealed type is "_ctypes.Array[__main__.MyCInt]"
3737
reveal_type(mya[0]) # N: Revealed type is "__main__.MyCInt"
3838
reveal_type(mya[1:3]) # N: Revealed type is "builtins.list[__main__.MyCInt]"
3939
mya[0] = 42
@@ -63,7 +63,7 @@ class MyCInt(ctypes.c_int):
6363
pass
6464

6565
mya: ctypes.Array[Union[MyCInt, ctypes.c_uint]]
66-
reveal_type(mya) # N: Revealed type is "ctypes.Array[Union[__main__.MyCInt, ctypes.c_uint]]"
66+
reveal_type(mya) # N: Revealed type is "_ctypes.Array[Union[__main__.MyCInt, ctypes.c_uint]]"
6767
reveal_type(mya[0]) # N: Revealed type is "Union[__main__.MyCInt, builtins.int]"
6868
reveal_type(mya[1:3]) # N: Revealed type is "builtins.list[Union[__main__.MyCInt, builtins.int]]"
6969
# The behavior here is not strictly correct, but intentional.
@@ -161,10 +161,10 @@ intarr4 = ctypes.c_int * 4
161161
intarr6 = ctypes.c_int * 6
162162
int_values = [1, 2, 3, 4]
163163
c_int_values = [ctypes.c_int(1), ctypes.c_int(2), ctypes.c_int(3), ctypes.c_int(4)]
164-
reveal_type(intarr4(*int_values)) # N: Revealed type is "ctypes.Array[ctypes.c_int]"
165-
reveal_type(intarr4(*c_int_values)) # N: Revealed type is "ctypes.Array[ctypes.c_int]"
166-
reveal_type(intarr6(1, ctypes.c_int(2), *int_values)) # N: Revealed type is "ctypes.Array[ctypes.c_int]"
167-
reveal_type(intarr6(1, ctypes.c_int(2), *c_int_values)) # N: Revealed type is "ctypes.Array[ctypes.c_int]"
164+
reveal_type(intarr4(*int_values)) # N: Revealed type is "_ctypes.Array[ctypes.c_int]"
165+
reveal_type(intarr4(*c_int_values)) # N: Revealed type is "_ctypes.Array[ctypes.c_int]"
166+
reveal_type(intarr6(1, ctypes.c_int(2), *int_values)) # N: Revealed type is "_ctypes.Array[ctypes.c_int]"
167+
reveal_type(intarr6(1, ctypes.c_int(2), *c_int_values)) # N: Revealed type is "_ctypes.Array[ctypes.c_int]"
168168
[typing fixtures/typing-medium.pyi]
169169

170170
float_values = [1.0, 2.0, 3.0, 4.0]

0 commit comments

Comments
 (0)