Skip to content

feat: runtime typecheck phase 2 - #5421

Open
mayankansys wants to merge 41 commits into
mainfrom
feat/Updated_runtime_phase
Open

mayankansys wants to merge 41 commits into
mainfrom
feat/Updated_runtime_phase

Conversation

@mayankansys

@mayankansys mayankansys commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Context

PR #5375 added runtime type-checking support. When beartype is applied to the package, many public APIs were silently left unchecked because beartype only warns on decoration failures. The no_runtime_type_check marker on classes also had no effect on methods - beartype checks each method individually.

Change Summary

  • Rewrote no_runtime_type_check to flag class methods, classmethods, staticmethods, properties, and nested classes.

  • Made PathType a real type (os.PathLike[str] | str) instead of a string.

  • Implemented PEP 695 type statements for recursive type aliases (ValueT, StateType, DictStateType, ListStateType) enabling both static checkers and runtime validators to see identical recursive definitions instead of TYPE_CHECKING splits.

  • Fixed mixed-quoted hints in field-data modules; imported descriptor classes at runtime.

  • Exempted helpers with unresolvable hints: _get_argvals_and_session, _get_running_session_mode, _set_state_safe, and abstract field-data classes.

  • Enhanced check_type() function to unwrap PEP 695 TypeAliasType objects, enabling proper validation of recursive type aliases at runtime.

  • Added comprehensive test coverage:

    - test_runtime_type_checking_public_api.py: Audits that all public APIs are decorated by beartype, wrapped correctly, and reject wrong types at runtime. Maintains allow-lists of exempt classes/functions with documentation.
    
    - test_runtime_type_checking_phase5.py: Validates that all core modules import successfully under whole-package beartype application, ensuring no decoration failures occur during module initialization.
    

Rationale

  • Beartype reads the flag on individual functions, not classes.
  • Runtime checkers resolve hints at call time, so they must be real types or importable names.
  • Circular imports require exempting some functions; concrete implementations stay checked.
  • A test is needed because beartype silently skips functions it cannot decorate.

Impact

  • Default: no change - no checker installed unless user enables one.
  • With a checker: launch_fluent, object-model, and field-data APIs are now checked; proxy classes remain exempt by design.
  • Maintainers: new test catches regressions in public API enforcement and module stability.

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@mayankansys mayankansys changed the title feat : runtime typecheck phase 2 feat: runtime typecheck phase 2 Oct 7, 2026
@mayankansys mayankansys linked an issue Oct 23, 2026 that may be closed by this pull request
return graphics_driver


@no_runtime_type_check

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why these decorators are needed on private modules? Isn't there a way to deal with it generically?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The private functions reference Meshing, Solver, and PureMeshing classes only under TYPE_CHECKING. Beartype can't resolve these names at runtime and would silently skip the functions. We exempt them explicitly to avoid noise and make the limitation clear in code.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

But is this function even called?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Right Sean. This is a helper function which was used by me later missed to remove it.

)


@no_runtime_type_check

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Again what's the standard for type checking abstract classes?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

There's no universal standard it's case-by-case. We exempt this class because its abstract methods mix unresolvable protocol types with beartype decoration.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This reply constitutes a useful code comment, so that future readers/maintainers understand the rationale.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

# These field-data contracts use annotations imported only under TYPE_CHECKING, such as SurfaceFieldDataRequest 
# and npt.NDArray. They remain available to static checkers, but beartype cannot resolve those names at runtime.
# That is why these contracts opt out; abstract classes do not generally need an exemption.

@github-actions github-actions Bot added maintenance General maintenance of the repo (libraries, cicd, etc) CI/CD labels Oct 28, 2026
@mayankansys
mayankansys marked this pull request as ready for review November 23, 2026 20:15
field_name: str | "ScalarVariableDescriptor"
additional_field_name: str | "ScalarVariableDescriptor" = ""
field_name: str | ScalarVariableDescriptor
additional_field_name: str = ""

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This drops the descriptor type, but we can see the descriptor validation just below here:
_validate_scalar_variable_descriptor(self.additional_field_name).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks @seanpearsonuk. I missed it.

@seanpearsonuk

Copy link
Copy Markdown
Collaborator

Avoid uppercase characters in branch names


from ansys.fluent.core._tools.settings.state_types import StateType
from ansys.fluent.core._type_checking import no_runtime_type_check
from ansys.fluent.core.solver.flobject import StateType

@seanpearsonuk seanpearsonuk Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This import creates a new dependency in the wrong direction. N.b. it's already defined in state_types so the issue is easily resolvable.

from ansys.fluent.core.solver.flobject import StateType


@no_runtime_type_check

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why is this excluded?

if TYPE_CHECKING:
import numpy as np
import numpy.typing as npt

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The following imports are presumably guarded to avoid a reverse dependency. That needs to be commented separately from the above imports.

:class:`~ansys.fluent.core.fields.field_data.live_field_data.LiveFieldData` is the
standard implementation.
"""

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

N.b. the following method is excluded but it doesn't seem necessary.

@seanpearsonuk

Copy link
Copy Markdown
Collaborator

_get_running_session_mode still appears in the description, but has been removed.

return ip, port, password


@no_runtime_type_check

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is it only the factory methods that are the issue here? Could you decorate those specifically instead?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI/CD maintenance General maintenance of the repo (libraries, cicd, etc) new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ensure support for runtime type-checking

4 participants