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

Add type annotations to clinic.py #104050

Open
erlend-aasland opened this issue May 1, 2023 · 7 comments
Open

Add type annotations to clinic.py #104050

erlend-aasland opened this issue May 1, 2023 · 7 comments
Assignees
Labels

Comments

@erlend-aasland
Copy link
Contributor

erlend-aasland commented May 1, 2023

Feature or enhancement

Gradually add type hints to clinic.py.

Pitch

Adding type hints to clinic.py will make it easier to debug, fix bugs, do code review, add new features, and triage. IMO, the churn is worth the effort in the long run.

Previous discussion

Since adding typing to the CPython code base has been a slightly controversial topic (obviously controversial for Lib/), I created a topic on Discourse and asked for feedback. So far, I have not received any discouragements :)

Linked PRs

@erlend-aasland erlend-aasland added type-feature A feature request or enhancement topic-typing labels May 1, 2023
@erlend-aasland erlend-aasland self-assigned this May 1, 2023
@erlend-aasland
Copy link
Contributor Author

erlend-aasland commented May 1, 2023

Since I've been ignorant to typing until now, I'd like to give this a shot myself; it'll be a nice opportunity to learn some new tricks.

EDIT:
Don't take this too serious; I don't want to block anybody from contributing. Please fire up PRs if you want (I appreciate if you tag me, though!)

@sobolevn
Copy link
Member

sobolevn commented May 3, 2023

@erlend-aasland feel free to ask for help/reviews :)

@hauntsaninja
Copy link
Contributor

A good place to start is by switching the instance fields in CConverter to use variable annotations. This is the cause of most of the existing mypy complaints.

As you add types, the type checker will become more useful. Once you've gotten a number of types in, I recommend upgrading to mypy --check-untyped-defs, and then possibly mypy --strict.

I'm biased, but I think mypy docs are a generally useful reference: https://mypy.readthedocs.io/en/latest/cheat_sheet_py3.html

@erlend-aasland
Copy link
Contributor Author

As recommended by @AlexWaygood, we should probably start by adding a minimal type check to the CI. (We really don't want to increase CI minutes too much.) In the beginning, perhaps an allow-list is the most manageable path forward. When that list starts to grow, we can consider switching to using a deny-list instead. Thoughts? PRs welcome :)

@AlexWaygood
Copy link
Member

AlexWaygood commented May 12, 2023

Here's a PR to add a mypy check in CI, checking only the Tools/clinic directory: #104421. The check completed in 20 seconds (and will only run on PRs that touch files in Tools/clinic/), so I doubt it will increase our use of CI minutes too much!

AlexWaygood added a commit that referenced this issue May 15, 2023
* Add basic mypy workflow to CI
* Make the type check pass

---------

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
carljm added a commit to carljm/cpython that referenced this issue May 15, 2023
* main: (29 commits)
  pythongh-101819: Fix _io clinic input for unused base class method stubs (python#104418)
  pythongh-101819: Isolate `_io` (python#101948)
  Bump mypy from 1.2.0 to 1.3.0 in /Tools/clinic (python#104501)
  pythongh-104494: Update certain Tkinter pack/place tests for Tk 8.7 errors (python#104495)
  pythongh-104050: Run mypy on `clinic.py` in CI (python#104421)
  pythongh-104490: Consistently define phony make targets (python#104491)
  pythongh-67056: document that registering/unregistering an atexit func from within an atexit func is undefined (python#104473)
  pythongh-104487: PYTHON_FOR_REGEN must be minimum Python 3.10 (python#104488)
  pythongh-101282: move BOLT config after PGO (pythongh-104493)
  pythongh-104469 Convert _testcapi/float.c to use AC (pythongh-104470)
  pythongh-104456: Fix ref leak in _ctypes.COMError (python#104457)
  pythongh-98539: Make _SSLTransportProtocol.abort() safe to call when closed (python#104474)
  pythongh-104337: Clarify random.gammavariate doc entry  (python#104410)
  Minor improvements to typing docs (python#104465)
  pythongh-87092: avoid gcc warning on uninitialized struct field in assemble.c (python#104460)
  pythonGH-71383: IDLE - Document testing subsets of modules (python#104463)
  pythongh-104454: Fix refleak in AttributeError_reduce (python#104455)
  pythongh-75710: IDLE - add docstrings and comments to editor module (python#104446)
  pythongh-91896: Revert some very noisy DeprecationWarnings for `ByteString` (python#104424)
  Add a mention of PYTHONBREAKPOINT to breakpoint() docs (python#104430)
  ...
carljm added a commit to carljm/cpython that referenced this issue May 15, 2023
* main: (204 commits)
  pythongh-101819: Fix _io clinic input for unused base class method stubs (python#104418)
  pythongh-101819: Isolate `_io` (python#101948)
  Bump mypy from 1.2.0 to 1.3.0 in /Tools/clinic (python#104501)
  pythongh-104494: Update certain Tkinter pack/place tests for Tk 8.7 errors (python#104495)
  pythongh-104050: Run mypy on `clinic.py` in CI (python#104421)
  pythongh-104490: Consistently define phony make targets (python#104491)
  pythongh-67056: document that registering/unregistering an atexit func from within an atexit func is undefined (python#104473)
  pythongh-104487: PYTHON_FOR_REGEN must be minimum Python 3.10 (python#104488)
  pythongh-101282: move BOLT config after PGO (pythongh-104493)
  pythongh-104469 Convert _testcapi/float.c to use AC (pythongh-104470)
  pythongh-104456: Fix ref leak in _ctypes.COMError (python#104457)
  pythongh-98539: Make _SSLTransportProtocol.abort() safe to call when closed (python#104474)
  pythongh-104337: Clarify random.gammavariate doc entry  (python#104410)
  Minor improvements to typing docs (python#104465)
  pythongh-87092: avoid gcc warning on uninitialized struct field in assemble.c (python#104460)
  pythonGH-71383: IDLE - Document testing subsets of modules (python#104463)
  pythongh-104454: Fix refleak in AttributeError_reduce (python#104455)
  pythongh-75710: IDLE - add docstrings and comments to editor module (python#104446)
  pythongh-91896: Revert some very noisy DeprecationWarnings for `ByteString` (python#104424)
  Add a mention of PYTHONBREAKPOINT to breakpoint() docs (python#104430)
  ...
erlend-aasland added a commit to erlend-aasland/cpython that referenced this issue May 16, 2023
erlend-aasland added a commit to erlend-aasland/cpython that referenced this issue May 16, 2023
sobolevn added a commit to sobolevn/cpython that referenced this issue May 16, 2023
erlend-aasland added a commit to erlend-aasland/cpython that referenced this issue May 16, 2023
@erlend-aasland
Copy link
Contributor Author

We should perhaps consider using protocols (for example, converters comes to mind).

carljm added a commit to carljm/cpython that referenced this issue May 16, 2023
* main:
  pythonGH-104510: Fix refleaks in `_io` base types (python#104516)
  pythongh-104539: Fix indentation error in logging.config.rst (python#104545)
  pythongh-104050: Don't star-import 'types' in Argument Clinic (python#104543)
  pythongh-104050: Add basic typing to CConverter in clinic.py (python#104538)
  pythongh-64595: Fix write file logic in Argument Clinic (python#104507)
  pythongh-104523: Inline minimal PGO rules (python#104524)
  pythongh-103861: Fix Zip64 extensions not being properly applied in some cases (python#103863)
  pythongh-69152: add method get_proxy_response_headers to HTTPConnection class (python#104248)
  pythongh-103763: Implement PEP 695 (python#103764)
  pythongh-104461: Run tkinter test_configure_screen on X11 only (pythonGH-104462)
  pythongh-104469: Convert _testcapi/watchers.c to use Argument Clinic (python#104503)
  pythongh-104482: Fix error handling bugs in ast.c (python#104483)
  pythongh-104341: Adjust tstate_must_exit() to Respect Interpreter Finalization (pythongh-104437)
  pythonGH-102613: Fix recursion error from `pathlib.Path.glob()` (pythonGH-104373)
carljm added a commit to carljm/cpython that referenced this issue May 17, 2023
* main: (26 commits)
  pythonGH-101520: Move tracemalloc functionality into core, leaving interface in Modules. (python#104508)
  typing: Add more tests for TypeVar (python#104571)
  pythongh-104572: Improve error messages for invalid constructs in PEP 695 contexts (python#104573)
  typing: Use PEP 695 syntax in typing.py (python#104553)
  pythongh-102153: Start stripping C0 control and space chars in `urlsplit` (python#102508)
  pythongh-104469: Update README.txt for _testcapi (pythongh-104529)
  pythonGH-103092: isolate `_elementtree` (python#104561)
  pythongh-104050: Add typing to Argument Clinic converters (python#104547)
  pythonGH-103906: Remove immortal refcounting in the interpreter (pythonGH-103909)
  pythongh-87474: Fix file descriptor leaks in subprocess.Popen (python#96351)
  pythonGH-103092: isolate `pyexpat`  (python#104506)
  pythongh-75367: Fix data descriptor detection in inspect.getattr_static (python#104517)
  pythongh-104050: Add more annotations to `Tools/clinic.py` (python#104544)
  pythongh-104555: Fix isinstance() and issubclass() for runtime-checkable protocols that use PEP 695 (python#104556)
  pythongh-103865: add monitoring support to LOAD_SUPER_ATTR (python#103866)
  CODEOWNERS: Assign new PEP 695 files to myself (python#104551)
  pythonGH-104510: Fix refleaks in `_io` base types (python#104516)
  pythongh-104539: Fix indentation error in logging.config.rst (python#104545)
  pythongh-104050: Don't star-import 'types' in Argument Clinic (python#104543)
  pythongh-104050: Add basic typing to CConverter in clinic.py (python#104538)
  ...
erlend-aasland added a commit to erlend-aasland/cpython that referenced this issue May 17, 2023
Introduce TypeSet, and use it to annotate the 'accept' keyword of
various C converters.
erlend-aasland added a commit to erlend-aasland/cpython that referenced this issue May 17, 2023
erlend-aasland added a commit that referenced this issue May 17, 2023
)

Introduce TypeSet, and use it to annotate the 'accept' keyword of
various C converters. Also add some missing return annotations for
converter init functions.
JelleZijlstra pushed a commit to JelleZijlstra/cpython that referenced this issue May 18, 2023
…python#104588)

Introduce TypeSet, and use it to annotate the 'accept' keyword of
various C converters. Also add some missing return annotations for
converter init functions.
@erlend-aasland
Copy link
Contributor Author

I amended my #104050 (comment); as stated in the edited text: I don't want to block anyone; please create PRs if you feel the urge :)

erlend-aasland added a commit to erlend-aasland/cpython that referenced this issue May 18, 2023
- class Class
- class Module
- class PythonParser
- function compute_checksum()
- function parse_file()
- variable unsupported_special_methods
AlexWaygood added a commit that referenced this issue May 18, 2023
…104626)

- Convert `unspecified` and `unknown` to be members of a `Sentinels` enum, rather than instances of bespoke classes.
  - An enum feels more idiomatic here, and works better with type checkers.
  - Convert some `==` and `!=` checks for these values to identity checks, which are more idiomatic with sentinels.
  - _Don't_ do the same for `Null`, as this needs to be a distinct type due to its usage in `clinic.py`.
- Use `object` as the annotation for `default` across `clinic.py`. `default` can be literally any object, so `object` is the correct annotation here.

---

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
erlend-aasland added a commit to erlend-aasland/cpython that referenced this issue May 18, 2023
Annotate methods of the following classes:

- class Function
- class Parameter
- class LandMine
erlend-aasland added a commit that referenced this issue May 18, 2023
Annotate the following:

- methods of class Class
- methods of class Module
- methods of class PythonParser
- function compute_checksum()
- function parse_file()
- global variable unsupported_special_methods
carljm added a commit to carljm/cpython that referenced this issue May 18, 2023
* main:
  pythongh-74690: Don't set special protocol attributes on non-protocol subclasses of protocols (python#104622)
  pythongh-104623: Update Windows installer to use SQLite 3.42.0 (python#104625)
  pythongh-104050: Add more type annotations to Argument Clinic (python#104628)
  pythongh-104629: Don't skip test_clinic if _testclinic is missing (python#104630)
  pythongh-104549: Set __module__ on TypeAliasType (python#104550)
  pythongh-104050: Improve some typing around `default`s and sentinel values (python#104626)
  pythongh-104146: Remove unused vars from Argument Clinic (python#104627)
  pythongh-104615: don't make unsafe swaps in apply_static_swaps (python#104620)
  pythonGH-104484: Add case_sensitive argument to `pathlib.PurePath.match()` (pythonGH-104565)
  pythonGH-96803: Document and test new unstable internal frame API functions (pythonGH-104211)
  pythonGH-104580: Don't cache eval breaker in interpreter (pythonGH-104581)
AlexWaygood added a commit to AlexWaygood/cpython that referenced this issue May 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants