Skip to content

Conversation

@junkmd
Copy link
Collaborator

@junkmd junkmd commented Dec 20, 2025

This pull request addresses a regression introduced in #885, where enforcing positional-only arguments for dispmethods led to SyntaxError in some generated type hints. This issue was discovered during Excel testing, which revealed cases where the generated code was syntactically invalid.

The DispMethodAnnotator would incorrectly generate signatures like def method(..., **kwargs, /) or def method(..., *args, **kwargs, /), both of which are invalid Python.

This PR includes two main changes:

  • A fix to DispMethodAnnotator to correctly place the positional-only marker (/) before **kwargs and to omit it entirely when *args is present.
  • A subsequent refactoring of the same method to simplify the logic and improve maintainability.

The `DispMethodAnnotator` was generating syntactically invalid
method signatures for dispmethods in two specific scenarios,
leading to `SyntaxError` when the generated type hint files were used.

First, when a method signature required `**kwargs` due to optional
arguments being followed by non-optional ones, the generated hint was
`def method(..., **kwargs: hints.Any, /)`. This is invalid syntax.

Second, when a method had an argument name that is a Python keyword,
the annotator would fall back to `*args: hints.Any, **kwargs: hints.Any`,
but would still incorrectly append a final `/`.

This commit corrects the logic in `DispMethodAnnotator.getvalue` to
properly handle the placement of the positional-only marker (`/`):
- The marker is now placed before `**kwargs` when present.
- The marker is omitted entirely when `*args` is in the signature, as a
  trailing slash is not valid in that context.

The test case in `test_typeannotator.py` has been updated to reflect
and verify these corrections.
The logic in `DispMethodAnnotator.getvalue` for placing the
positional-only argument marker (`/`) was complex. It involved several
conditional checks after the main argument list had been constructed.

This commit refactors the method to integrate the marker placement
directly into the `inargs` construction loop.
@junkmd junkmd added this to the 1.4.15 milestone Dec 20, 2025
@junkmd junkmd added bug Something isn't working typing related to Python static typing system labels Dec 20, 2025
@codecov
Copy link

codecov bot commented Dec 20, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.00%. Comparing base (538c0d4) to head (a6533e2).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #887   +/-   ##
=======================================
  Coverage   85.00%   85.00%           
=======================================
  Files         126      126           
  Lines       11695    11698    +3     
=======================================
+ Hits         9941     9944    +3     
  Misses       1754     1754           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@junkmd junkmd merged commit ece82ed into enthought:main Dec 20, 2025
52 checks passed
@junkmd junkmd deleted the fix_safeguards_for_unsupported_named_params branch December 20, 2025 03:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working typing related to Python static typing system

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant