Skip to content

Fix ty unresolved-attribute warnings and add mypy back - #1737

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

Fix ty unresolved-attribute warnings and add mypy back#1737
tleonhardt wants to merge 19 commits into
mainfrom
ty-unresolved-attribute

Conversation

@tleonhardt

@tleonhardt tleonhardt commented Aug 26, 2026

Copy link
Copy Markdown
Member

Add mypy back in addition to ty for type checking.

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

Major changes:

  • Add mypy back so now both ty and mypy are used for type checking - each are better at some things
  • 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 (9699b2b).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1737      +/-   ##
==========================================
- Coverage   99.66%   99.64%   -0.02%     
==========================================
  Files          23       23              
  Lines        5937     5960      +23     
==========================================
+ Hits         5917     5939      +22     
- 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)

…ommandFunc Protocol classes

Also:
- Switched some types in cmd2.py from `Callable[..., Any]` to `BoundCommandFunc`
- Removed a number of `cast` calls in cmd2.py which were no longer needed
…unc and UnboundCommand func

Also:
- Restored the simple attribute definition for `__name__` attribute in BoundCommandFunc and UnboundCommandFund
Comment thread cmd2/types.py Outdated
Comment thread cmd2/annotated.py Outdated
…ntation

Deleted _CommandFunc from annotated.py and replaced with BoundCommandFunc from types.py
It turns out that ty is better at some things and mypy is better at others when it comes to type checking. So ty is at least not yet a purely superior replacement for mypy.
Most modern agent harness CLIs like codex, agy, opencode, hemes, pi, oh-my-pi, and tau all read AGENTS.md by default. It has emerged as the single file used by most agentic AI tools.

Leaving GEMINI.md in place for now since Gemini CLI doesn't read AGENTS.md by default.
@tleonhardt tleonhardt changed the title Fix ty unresolved-attribute warnings and stop globally ignoring them Fix ty unresolved-attribute warnings and add mypy back Aug 28, 2026
@tleonhardt

Copy link
Copy Markdown
Member Author

@KelvinChung2000 @kmvanbrunt Ok, I think this is ready for another review.

I ended up adding mypy back to our type checking in addition to ty because each of them are stricter about checking some things than the other.

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.

3 participants