Skip to content

Explicit attribute type annotations#825

Merged
hynek merged 1 commit into
hynek:mainfrom
jorenham:typing/attribute-annotations
Jul 18, 2026
Merged

Explicit attribute type annotations#825
hynek merged 1 commit into
hynek:mainfrom
jorenham:typing/attribute-annotations

Conversation

@jorenham

@jorenham jorenham commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Summary

Hi there,

I see that you're serious about static typing here; awesome!

When I ran pyrefly coverage check on the codebase, I noticed some opportunities for improvements because the reported type coverage (on main) is 93.49% (or 95.23% if you only consider the public API with --public-only).
So I figured I might as well contribute, and well, here I am 🤷.

This adds explicit type annotations for the instance attributes, so that there's no need anymore to hope that all type-checkers infer the same intended types for them.
With this, the type coverage increased by +3.2% for a grand total of 96.68% (or with +3.66% to 98.89% with --public-only) 🎉.
These attribute annotations can also be considered as a form of documentation, because now you can easily see what attributes a certain class has and what their types are, without having to find the __init__ parse manually parse the logic in there.

Pull Request Checklist

  • I acknowledge this project's AI policy.
  • This pull request is not from my main branch.
  • There's tests for all new and changed code.
  • New APIs are added to our typing tests in api.py.
  • Updated documentation for changed code.
    • New functions/classes have to be added to docs/api.rst by hand.
    • Changed/added classes/methods/functions have appropriate versionadded, versionchanged, or deprecated directives.
      • The next version is the second number in the current release + 1. The first number represents the current year. So if the current version on PyPI is 26.1.0, the next version is gonna be 26.2.0. If the next version is the first in the new year, it'll be 27.1.0.
  • Documentation in .rst and .md files is written using semantic newlines.
  • Changes (and possible deprecations) are documented in the changelog.

No AI was used; not even a little bit :)

I'm not sure if this is something you thing is worth putting in the changelog, so let me know if I should.

@hynek

hynek commented Jul 18, 2026

Copy link
Copy Markdown
Owner

ha nice thanks! I guess this is how one sees by how much structlog predates type hints. Given the dynamic nature of structlog, it was a PITA to add in the first place.

What kinds of annotations are missing still?

No AI was used; not even a little bit :)

I cried a little bit 🥲

@hynek
hynek merged commit 4b10c94 into hynek:main Jul 18, 2026
18 checks passed
@jorenham
jorenham deleted the typing/attribute-annotations branch July 18, 2026 09:19
@jorenham

Copy link
Copy Markdown
Contributor Author

I guess this is how one sees by how much structlog predates type hints. Given the dynamic nature of structlog, it was a PITA to add in the first place.

I've been working on improving static typing in NumPy for 2 years now, so I feel you 😅

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