Skip to content

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

Closed

Description

@junkmd

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.

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 delete _importlib_modulespec #5350, were merged as "should be fine in pyi files" even if they are circular imports.

Any opinions would be appreciated.

Activity

  1. hauntsaninja commented on Aug 29, 2022

    @hauntsaninja
    Collaborator

    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.

  2. junkmd commented on Aug 29, 2022

    @junkmd
    ContributorAuthor

    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.

  3. junkmd commented on Aug 30, 2022

    @junkmd
    ContributorAuthor

    I submitted issue to pytype google/pytype#1281

  4. junkmd commented on Aug 30, 2022

    @junkmd
    ContributorAuthor

    The pytype problem causes by its own _ctypes stub.

    This will be fixed "in a few weeks".

  5. junkmd commented on Aug 30, 2022

    @junkmd
    ContributorAuthor

    List of the stuffs in ctypes/__init__.pyi those what originally defined in _ctypes

    marked stuffs have been migrated.

    • ArgumentError
    • Array
    • FormatError
    • POINTER
    • RTLD_GLOBAL
    • RTLD_LOCAL
    • Structure
    • Union
    • _Pointer
    • _SimpleCData
      • _CData(hidden base class)
    • addressof
    • alignment
    • byref
    • get_errno
    • get_last_error
    • pointer
    • resize
    • set_errno
    • set_last_error
    • sizeof
  6. rchen152 commented on Aug 30, 2022

    @rchen152
    Collaborator

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

  7. junkmd commented on Aug 31, 2022

    @junkmd
    ContributorAuthor

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

  8. AlexWaygood commented on Aug 31, 2022

    @AlexWaygood
    Member

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

  9. AlexWaygood commented on Apr 29, 2023

    @AlexWaygood
    Member

    I just updated typeshed's pinned version of pytype to 2023.4.27 in #10111. pytype v2023.4.27 includes google/pytype@2e2088f, 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.

  10. junkmd commented on May 3, 2023

    @junkmd
    ContributorAuthor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions