Skip to content

Conversation

@rwb27
Copy link
Collaborator

@rwb27 rwb27 commented Jan 28, 2026

Previously, BaseDescriptor typed the owning object as Thing, which was a bit loose and led to some vague type hints (particularly for functional properties and actions).

I've added a second generic parameter for the owning class. This makes some of the type test code a bit more verbose, but it gets rid of a fair few type: ignore statements and also means we now detect a few type errors that were previously missed.

This is mostly tidying up, but will be useful for some PRs in the near future.

Closes #217
Closes #162

rwb27 added 5 commits January 28, 2026 16:53
Previously, BaseDescriptor typed the owning object as `Thing`, which was
a bit loose and led to some vague type hints (particularly for functional
properties and actions).

I've added a second generic parameter for the owning class. This makes some of the type test code
a bit more verbose, but it gets rid of
a fair few `type: ignore` statements
and also means we now detect a few
type errors that were previously missed.

This is mostly tidying up, but will be useful for some PRs in the near future.
This uses the new generic parameter for the descriptor's Owner to correctly type the function's `self` parameter, among other things.
This was a one-character fix: we now need the second argument when
inspecting `__orig_class__` to get the
value type.

This is only used when a descriptor is created using subscript syntax, e.g.
`prop = Property[Thing, int](default=0)`.

Happily, it was picked up in tests, and is now passing.
This now correctly uses the `Owner` type variable in `BaseDescriptor`. `mypy` picked up an inconsistency between this and `ActionDescriptor`, but oddly not on Python 3.10.
@barecheck
Copy link

barecheck bot commented Jan 28, 2026

Barecheck - Code coverage report

Total: 96.31%

Your code coverage diff: 0.01% ▴

Uncovered files and lines
FileLines
src/labthings_fastapi/actions.py162-163, 414-415, 539-540, 553, 578-579, 704, 850-851, 854, 857, 905
src/labthings_fastapi/properties.py675, 679, 702-705, 777, 796, 929

@rwb27 rwb27 requested a review from julianstirling February 2, 2026 09:39
Copy link
Contributor

@julianstirling julianstirling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I am confused about that could do with a comment. Otherwise this seem like a significant improvement in type specificity.

@rwb27 rwb27 force-pushed the generic-owner-for-descriptors branch from 4979167 to 93dd2dc Compare February 2, 2026 23:37
@rwb27 rwb27 merged commit 794fd23 into main Feb 2, 2026
14 checks passed
@rwb27 rwb27 deleted the generic-owner-for-descriptors branch February 2, 2026 23:39
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.

Consider making BaseDescriptor generic with respect to the host class. A test in typing_tests/thing_definitions.py passes and I don't understand why.

3 participants