Type checking with ty.
#2707
Replies: 2 comments 1 reply
-
|
I think the key difference between now and #665 is the existence of ty - being able to run over all of Briefcase in 0.16s is... a lot faster than mypy :-) I have some reservations based on the maturity of ty - I'm a fan of boring tools, and tools that are still in the early phases of development tend to be a little too "spicy". Any bug they have may be fixed quickly - but while it's present, we need to deal with it, and our contributors need to deal with it, and so on. The current state of their version policy suggests that even Astral thinks ty is an unstable platform at this point. I also have some reservations based on the fact that the thing typing finds abhorrent - dynamic attributes - is something that is extremely useful. As you've noted, the platform module is a particular edge case here, because each backend has properties and attributes that are extremely platform specific. We don't really want to annotate all the Linux-specific properties onto a base class so that macOS/Windows users have to navigate around them - if only because there will almost certainly be cases where the types are inconsistent between platforms. My concern here is typing become a "technically correct" straightjacket where our lives as developers becomes unwieldy just to keep a type checker happy. However - if there is a path to progressively introducing type checks, and the tool won't be cumbersome until we choose to enforce it in the places where we can, then I think it's at least worth the experiment. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the honest response. Addressing the two concerns directly:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
While working on the publication channels plugin interface (#2701), I leaned on Protocols and type annotations to define the plugin API. It got me thinking about whether the project could benefit from a type checker — especially now that there's a fast one available.
Type checking has come up before (#665). The response at the time was that type annotations aren't an inherent good — they can help as documentation but can also impede readability, and a broader project-wide conversation was needed.
ty might make that conversation worth revisiting.
Since the project already uses
ruffandtox-uv, the Astral toolchain is familiar territory. The speed means ty can run in pre-commit without anyone noticing it's there.I ran it on briefcase as an experiment:
src/briefcase/treeunresolved-attribute— mostly fromBaseConfigbeing loosely typedinvalid-argument-type, 24invalid-return-type— potentially real catchesunresolved-import— conditional imports (tomli,dmgbuild)The 597 number looks scary but the story is simple: one typing gap —
BaseConfig/AppConfigattributes being set dynamically — accounts for ~75% of the noise. The top offenders are platform modules (macOS/__init__.pyalone has 164). Fixing the type forAppConfigwould eliminate most of these in one go. The remaining ~150 are a mix of real catches (e.g. acreate() -> boolwith a code path that implicitly returnsNone) and places where ty is stricter than the runtime.The codebase is already in decent shape for this: ~60% of files have
from __future__ import annotations. The integrations layer was typed in 2022–2023, and newer plugin interfaces (channels, debuggers) use Protocols and proper annotations.Proposal: gradual introduction
This doesn't need to be all-or-nothing. ty supports per-rule severity (
--error,--warn,--ignore), file exclusions, and configuration viapyproject.toml— so it can be introduced gradually:channels/orintegrations/base.pythat's already well-typed, and make ty enforced just there.BaseConfigattribute access patterns). Gives contributors a clear picture of what's expected where.Is there appetite for exploring this? Happy to propose more detailed roadmap.
Beta Was this translation helpful? Give feedback.
All reactions