Skip to content

Fix ty unresolved-attribute warnings and stop globally ignoring them - #1737

Open
tleonhardt wants to merge 4 commits into
mainfrom
ty-unresolved-attribute
Open

Fix ty unresolved-attribute warnings and stop globally ignoring them#1737
tleonhardt wants to merge 4 commits into
mainfrom
ty-unresolved-attribute

Conversation

@tleonhardt

@tleonhardt tleonhardt commented Aug 26, 2026

Copy link
Copy Markdown
Member

Fix ty unresolved-attribute warnings and stop globally ignoring them within ty.toml.

Major changes:

  • Convert BoundCommandFunc and UnboundCommandFunc TypeAliases in types.py to Protocol classes for stricter type checking
  • Added _CommandFunc type alias in annotated.py based on BoundCommandFunc and UnboundCommandFunc
  • Used getattr and/or cast() to help resolve some type errors in cmd2.py

Minor changes:

  • Added type ignore for ty:unresolved-attribute to a number of places we were already ignoring attr-defined for mypy (problem of different name for same type of check)

Major changes:
- Convert BoundCommandFunc and UnboundCommandFunc TypeAliases in types.py to Protocol classes for stricter type checking
- Added `_NamedCallable` Protocol class in annotated.py for stricter type checking of function references
- Used `getattr` and/or `cast()` to help resolve some type errors in cmd2.py

Minor changes:
- Added type ignore for `ty:unresolved-attribute` to a number of places we were already ignoring `attr-defined` for mypy (problem of different name for same type of check)
@tleonhardt tleonhardt self-assigned this Aug 26, 2026
@tleonhardt
tleonhardt requested a review from kmvanbrunt as a code owner August 26, 2026 02:30
@codecov

codecov Bot commented Aug 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.64%. Comparing base (5dcbcbe) to head (59455b6).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1737      +/-   ##
==========================================
- Coverage   99.66%   99.64%   -0.02%     
==========================================
  Files          23       23              
  Lines        5937     5949      +12     
==========================================
+ Hits         5917     5928      +11     
- Misses         20       21       +1     
Flag Coverage Δ
unittests 99.64% <100.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

Changes include:
1. Removed _NamedCallable: Deleted the redundant protocol class definition of _NamedCallable from cmd2/annotated.py.

2. Imported type protocols: Imported BoundCommandFunc and UnboundCommandFunc from cmd2/types.py, and TypeAlias from typing.

3. Defined unified _CommandFunc alias: Formed a private, unified type alias _CommandFunc = BoundCommandFunc | UnboundCommandFunc[CmdOrSetT, [argparse.Namespace]].

4. Updated function signatures: Replaced all annotations that previously used _NamedCallable in cmd2/annotated.py with _CommandFunc.
@tleonhardt

Copy link
Copy Markdown
Member Author

@KelvinChung2000 Could you please review the changes in annotated.py? There this is mostly replacing every Callable[..., Any] type hint with a type alias: _CommandFunc: TypeAlias = BoundCommandFunc | UnboundCommandFunc[CmdOrSetT, Any] so it is more in line with type hints elsewhere in the codebase.

@kmvanbrunt Could you please review changes outside of annotated.py? For this there are two main changes:

  1. Account for ty calling what mypy called attr-defined, unresolved-attribute instead.
  2. Converted BoundCommandFunc and UnboundCommandFunc from simple type alias to Protocol classes which do a better job of type hinting that certain attributes have to exist (duck typing)

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

Successfully merging this pull request may close these issues.

2 participants