migrate some stuffs in `ctypes/__init__.pyi` into `_ctypes.pyi`, but circular import occurs
When we submitted PR that adds the stdlib/_ctypes.pyi(#8582), imports some stuffs from ctypes.
But the implementation imports some stuffs from _ctypes to ctypes.
- also related to https://github.com/python/typeshed/issues/8571#issuecomment-1221316973
However, trying to write type stubs as close as possible to the implementation would result in circular imports if we just move definitions around.
- For example,
CDLLin args of_CDatamethods.- In implementation...
CDLLLis defined inctypes_CData(base class of_SimpleCData) is defined in_ctypes.
- In implementation...
- Some PRs, such as #5350, were merged as "should be fine in
pyifiles" even if they are circular imports.
Any opinions would be appreciated.
You don't really need to worry about circular imports, since stub files are never imported by Python. All type checkers know how to resolve circular imports when resolving references. That said, in this case you could probably move the fake definitions _CArgObject, _PointerLike to _ctypes and import them in ctypes.
When we tried to make the change, we found that the test using pytype running in CI failed.
Please see #8643.
I will work on adding TODO comments to the stuffs originally defined in _ctypes until pytype is fixed or better workarounds are found.
I submitted issue to pytype google/pytype#1281
List of the stuffs in ctypes/__init__.pyi those what originally defined in _ctypes
marked stuffs have been migrated.
- [x]
ArgumentError - [x]
Array - [x]
FormatError - [x]
POINTER - [x]
RTLD_GLOBAL - [x]
RTLD_LOCAL - [x]
Structure - [x]
Union - [x]
_Pointer - [x]
_SimpleCData- [x]
_CData(hidden base class)
- [x]
- [x]
addressof - [x]
alignment - [x]
byref - [x]
get_errno - [x]
get_last_error - [x]
pointer - [x]
resize - [x]
set_errno - [x]
set_last_error - [x]
sizeof
If you update to pytype 2022.08.30, you should be able to import RTLD_GLOBAL and RTLD_LOCAL from _ctypes now. (Just to clarify, what will happen "in a few weeks" is that I'll delete pytype's _ctypes stub so that it no longer has to be kept in sync with typeshed's.)
Currently, RTLD_GLOBAL and RTLD_LOCAL are imported from _ctypes.pyi(#8656).
Yes, the pytype update did the trick — thanks v much @rchen152!
I just updated typeshed's pinned version of pytype to 2023.4.27 in #10111. pytype v2023.4.27 includes https://github.com/google/pytype/commit/2e2088fcae3767802fe60f419499090db59f6c4f, which removes pytype's copy of stdlib/_ctypes.pyi. This means we can now move the rest of the definitions that should be in _ctypes.pyi (but are currently in ctypes/__init__.pyi) over to _ctypes.pyi.
#10118, #10125, #10128, #10130, #10133 and #10137 have been merged and the definition movings are completed.
The CFuncPtr and the _FuncPointer will be addressed within the scope of #8968 and #10132.