Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
junkmd opened this issue Aug 27, 2022 · 10 comments

Comments

@junkmd
Copy link
Contributor

junkmd commented Aug 27, 2022

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.

@hauntsaninja
Copy link
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.

@junkmd
Copy link
Contributor Author

junkmd commented Aug 29, 2022

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
Copy link
Contributor Author

junkmd commented Aug 30, 2022

I submitted issue to pytype google/pytype#1281

@junkmd
Copy link
Contributor Author

junkmd commented Aug 30, 2022

The pytype problem causes by its own _ctypes stub.

This will be fixed "in a few weeks".

@junkmd
Copy link
Contributor Author

junkmd commented Aug 30, 2022

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

@rchen152
Copy link
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.)

@junkmd
Copy link
Contributor Author

junkmd commented Aug 31, 2022

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

@AlexWaygood
Copy link
Member

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

@AlexWaygood
Copy link
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.

@junkmd
Copy link
Contributor Author

junkmd commented May 3, 2023

#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 junkmd closed this as completed May 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants