typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

migrate some stuffs in `ctypes/__init__.pyi` into `_ctypes.pyi`, but circular import occurs

Open junkmd opened this issue 3 years ago • 8 comments

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, CDLL in args of _CData methods.
    • In implementation...
      • CDLLL is defined in ctypes
      • _CData(base class of _SimpleCData) is defined in _ctypes.
  • Some PRs, such as #5350, were merged as "should be fine in pyi files" even if they are circular imports.

Any opinions would be appreciated.

junkmd avatar Aug 27 '22 11:08 junkmd

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.

hauntsaninja avatar Aug 29 '22 01:08 hauntsaninja

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.

junkmd avatar Aug 29 '22 14:08 junkmd

I submitted issue to pytype google/pytype#1281

junkmd avatar Aug 30 '22 01:08 junkmd

The pytype problem causes by its own _ctypes stub.

This will be fixed "in a few weeks".

junkmd avatar Aug 30 '22 11:08 junkmd

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] 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

junkmd avatar Aug 30 '22 13:08 junkmd

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.)

rchen152 avatar Aug 30 '22 23:08 rchen152

Currently, RTLD_GLOBAL and RTLD_LOCAL are imported from _ctypes.pyi(#8656).

junkmd avatar Aug 31 '22 11:08 junkmd

Yes, the pytype update did the trick — thanks v much @rchen152!

AlexWaygood avatar Aug 31 '22 11:08 AlexWaygood

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.

AlexWaygood avatar Apr 29 '23 17:04 AlexWaygood

#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.

junkmd avatar May 03 '23 23:05 junkmd