Skip to content

fix: Ignore NameError when introspecting plan functions - #1575

Merged
tpoliaw merged 1 commit into
mainfrom
field-type-hints
Jun 26, 2026
Merged

tpoliaw merged 1 commit into
mainfrom
field-type-hints

Conversation

@tpoliaw

@tpoliaw tpoliaw commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

fix: Ignore NameError when introspecting plan functions

When getting type hints to determine if a function is a plan, a
NameError can be raised (specifically when checking pydantic.Field
in this case) and the context loading crashes. Ignoring the error in the
same way that we ignore TypeError allows us to ignore broken types.

As we are only interested in specific return types (Generator) this
shouldn't affect which functions are treated as plans.

Specific symptoms/causes:
In pydantic Field has an argument with a type hint of (via an alias or
two) JsonValue. This is defined as a recursive alias

JsonValue: TypeAlias = Union[int, float, str, bool, None, list['JsonValue'], 'JsonDict']
JsonDict: TypeAlias = dict[str, JsonValue]

when get_type_hints is called on Field a NameError is raised.

This can be seen in a python repl without any blueapi related code

>>> from typing import get_type_hints
>>> from pydantic import Field
>>> get_type_hints(Field)
Traceback (most recent call last):
...
NameError: name 'JsonValue' is not defined

In blueapi, this is a problem if any module included via a PlanSource
has Field as a top level object.

@tpoliaw
tpoliaw requested a review from a team as a code owner June 26, 2026 13:13
@tpoliaw
tpoliaw force-pushed the field-type-hints branch from 9bf99b0 to 4bf2f92 Compare June 26, 2026 13:15

@abbiemery abbiemery left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you elaborate on the comment re pydantic.Field and jsonValue so we don't have to discover this again

@codecov

codecov Bot commented Jun 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.93%. Comparing base (7622ee0) to head (cb6c788).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1575   +/-   ##
=======================================
  Coverage   95.93%   95.93%           
=======================================
  Files          44       44           
  Lines        3269     3269           
=======================================
  Hits         3136     3136           
  Misses        133      133           

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

When getting type hints to determine if a function is a plan, a
`NameError` can be raised (specifically when checking `pydantic.Field`
in this case) and the context loading crashes. Ignoring the error in the
same way that we ignore `TypeError` allows us to ignore broken types.

As we are only interested in specific return types (`Generator`) this
shouldn't affect which functions are treated as plans.

Specific symptoms/causes:
In pydantic `Field` has an argument with a type hint of (via an alias or
two) `JsonValue`. This is defined as a recursive alias

    JsonValue: TypeAlias = Union[int, float, str, bool, None, list['JsonValue'], 'JsonDict']
    JsonDict: TypeAlias = dict[str, JsonValue]

when `get_type_hints` is called on `Field` a `NameError` is raised.

This can be seen in a python repl

    >>> from typing import get_type_hints
    >>> from pydantic import Field
    >>> get_type_hints(Field)
    Traceback (most recent call last):
    ...
    NameError: name 'JsonValue' is not defined

In blueapi, this is a problem if any module included via a PlanSource
has `Field` as a top level object.
@tpoliaw
tpoliaw force-pushed the field-type-hints branch from 4bf2f92 to cb6c788 Compare June 26, 2026 13:41
@tpoliaw
tpoliaw requested a review from abbiemery June 26, 2026 13:42
@tpoliaw
tpoliaw enabled auto-merge June 26, 2026 13:55
@tpoliaw
tpoliaw added this pull request to the merge queue Jun 26, 2026
Merged via the queue into main with commit 315665f Jun 26, 2026
22 checks passed
@tpoliaw
tpoliaw deleted the field-type-hints branch June 26, 2026 14:00
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