Skip to content

Commit

Permalink
Fix ctypes plugin (hopefully)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood authored and JelleZijlstra committed May 1, 2023
1 parent b1761f4 commit 17b4693
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
8 changes: 4 additions & 4 deletions mypy/plugins/ctypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ def _find_simplecdata_base_arg(
None is returned if _SimpleCData appears nowhere in tp's (direct or indirect) bases.
"""
if tp.type.has_base("ctypes._SimpleCData"):
if tp.type.has_base("_ctypes._SimpleCData"):
simplecdata_base = map_instance_to_supertype(
tp,
api.named_generic_type("ctypes._SimpleCData", [AnyType(TypeOfAny.special_form)]).type,
api.named_generic_type("_ctypes._SimpleCData", [AnyType(TypeOfAny.special_form)]).type,
)
assert len(simplecdata_base.args) == 1, "_SimpleCData takes exactly one type argument"
return get_proper_type(simplecdata_base.args[0])
Expand Down Expand Up @@ -88,7 +88,7 @@ def _autounboxed_cdata(tp: Type) -> ProperType:
return make_simplified_union([_autounboxed_cdata(t) for t in tp.items])
elif isinstance(tp, Instance):
for base in tp.type.bases:
if base.type.fullname == "ctypes._SimpleCData":
if base.type.fullname == "_ctypes._SimpleCData":
# If tp has _SimpleCData as a direct base class,
# the auto-unboxed type is the single type argument of the _SimpleCData type.
assert len(base.args) == 1
Expand All @@ -102,7 +102,7 @@ def _get_array_element_type(tp: Type) -> ProperType | None:
"""Get the element type of the Array type tp, or None if not specified."""
tp = get_proper_type(tp)
if isinstance(tp, Instance):
assert tp.type.fullname == "ctypes.Array"
assert tp.type.fullname == "_ctypes.Array"
if len(tp.args) == 1:
return get_proper_type(tp.args[0])
return None
Expand Down
12 changes: 6 additions & 6 deletions mypy/plugins/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class DefaultPlugin(Plugin):
def get_function_hook(self, fullname: str) -> Callable[[FunctionContext], Type] | None:
from mypy.plugins import ctypes, singledispatch

if fullname == "ctypes.Array":
if fullname == "_ctypes.Array":
return ctypes.array_constructor_callback
elif fullname == "functools.singledispatch":
return singledispatch.create_singledispatch_function_callback
Expand Down Expand Up @@ -69,7 +69,7 @@ def get_method_signature_hook(
return typed_dict_pop_signature_callback
elif fullname in {n + ".update" for n in TPDICT_FB_NAMES}:
return typed_dict_update_signature_callback
elif fullname == "ctypes.Array.__setitem__":
elif fullname == "_ctypes.Array.__setitem__":
return ctypes.array_setitem_callback
elif fullname == singledispatch.SINGLEDISPATCH_CALLABLE_CALL_METHOD:
return singledispatch.call_singledispatch_function_callback
Expand All @@ -92,9 +92,9 @@ def get_method_hook(self, fullname: str) -> Callable[[MethodContext], Type] | No
return typed_dict_pop_callback
elif fullname in {n + ".__delitem__" for n in TPDICT_FB_NAMES}:
return typed_dict_delitem_callback
elif fullname == "ctypes.Array.__getitem__":
elif fullname == "_ctypes.Array.__getitem__":
return ctypes.array_getitem_callback
elif fullname == "ctypes.Array.__iter__":
elif fullname == "_ctypes.Array.__iter__":
return ctypes.array_iter_callback
elif fullname == singledispatch.SINGLEDISPATCH_REGISTER_METHOD:
return singledispatch.singledispatch_register_callback
Expand All @@ -105,9 +105,9 @@ def get_method_hook(self, fullname: str) -> Callable[[MethodContext], Type] | No
def get_attribute_hook(self, fullname: str) -> Callable[[AttributeContext], Type] | None:
from mypy.plugins import ctypes, enums

if fullname == "ctypes.Array.value":
if fullname == "_ctypes.Array.value":
return ctypes.array_value_callback
elif fullname == "ctypes.Array.raw":
elif fullname == "_ctypes.Array.raw":
return ctypes.array_raw_callback
elif fullname in enums.ENUM_NAME_ACCESS:
return enums.enum_name_callback
Expand Down
14 changes: 7 additions & 7 deletions test-data/unit/check-ctypes.test
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class MyCInt(ctypes.c_int):
intarr4 = ctypes.c_int * 4
a = intarr4(1, ctypes.c_int(2), MyCInt(3), 4)
intarr4(1, 2, 3, "invalid") # E: Array constructor argument 4 of type "str" is not convertible to the array element type "c_int"
reveal_type(a) # N: Revealed type is "ctypes.Array[ctypes.c_int]"
reveal_type(a) # N: Revealed type is "_ctypes.Array[ctypes.c_int]"
reveal_type(a[0]) # N: Revealed type is "builtins.int"
reveal_type(a[1:3]) # N: Revealed type is "builtins.list[builtins.int]"
a[0] = 42
Expand All @@ -33,7 +33,7 @@ myintarr4 = MyCInt * 4
mya = myintarr4(1, 2, MyCInt(3), 4)
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" \
# E: Array constructor argument 4 of type "str" is not convertible to the array element type "MyCInt"
reveal_type(mya) # N: Revealed type is "ctypes.Array[__main__.MyCInt]"
reveal_type(mya) # N: Revealed type is "_ctypes.Array[__main__.MyCInt]"
reveal_type(mya[0]) # N: Revealed type is "__main__.MyCInt"
reveal_type(mya[1:3]) # N: Revealed type is "builtins.list[__main__.MyCInt]"
mya[0] = 42
Expand Down Expand Up @@ -63,7 +63,7 @@ class MyCInt(ctypes.c_int):
pass

mya: ctypes.Array[Union[MyCInt, ctypes.c_uint]]
reveal_type(mya) # N: Revealed type is "ctypes.Array[Union[__main__.MyCInt, ctypes.c_uint]]"
reveal_type(mya) # N: Revealed type is "_ctypes.Array[Union[__main__.MyCInt, ctypes.c_uint]]"
reveal_type(mya[0]) # N: Revealed type is "Union[__main__.MyCInt, builtins.int]"
reveal_type(mya[1:3]) # N: Revealed type is "builtins.list[Union[__main__.MyCInt, builtins.int]]"
# The behavior here is not strictly correct, but intentional.
Expand Down Expand Up @@ -161,10 +161,10 @@ intarr4 = ctypes.c_int * 4
intarr6 = ctypes.c_int * 6
int_values = [1, 2, 3, 4]
c_int_values = [ctypes.c_int(1), ctypes.c_int(2), ctypes.c_int(3), ctypes.c_int(4)]
reveal_type(intarr4(*int_values)) # N: Revealed type is "ctypes.Array[ctypes.c_int]"
reveal_type(intarr4(*c_int_values)) # N: Revealed type is "ctypes.Array[ctypes.c_int]"
reveal_type(intarr6(1, ctypes.c_int(2), *int_values)) # N: Revealed type is "ctypes.Array[ctypes.c_int]"
reveal_type(intarr6(1, ctypes.c_int(2), *c_int_values)) # N: Revealed type is "ctypes.Array[ctypes.c_int]"
reveal_type(intarr4(*int_values)) # N: Revealed type is "_ctypes.Array[ctypes.c_int]"
reveal_type(intarr4(*c_int_values)) # N: Revealed type is "_ctypes.Array[ctypes.c_int]"
reveal_type(intarr6(1, ctypes.c_int(2), *int_values)) # N: Revealed type is "_ctypes.Array[ctypes.c_int]"
reveal_type(intarr6(1, ctypes.c_int(2), *c_int_values)) # N: Revealed type is "_ctypes.Array[ctypes.c_int]"
[typing fixtures/typing-medium.pyi]

float_values = [1.0, 2.0, 3.0, 4.0]
Expand Down

0 comments on commit 17b4693

Please sign in to comment.