Skip to content

Fix ThermoPhase.atomic_weight typing mistake#2024

Merged
ischoegl merged 2 commits intoCantera:mainfrom
TimothyEDawson:fix-element-index-typing
Oct 30, 2025
Merged

Fix ThermoPhase.atomic_weight typing mistake#2024
ischoegl merged 2 commits intoCantera:mainfrom
TimothyEDawson:fix-element-index-typing

Conversation

@TimothyEDawson
Copy link
Copy Markdown
Contributor

Changes proposed in this pull request

Missed that ThermoPhase.atomic_weight calls ThermoPhase.element_index behind the scenes, so here's a quick fix.

Checklist

  • The pull request includes a clear description of this code change
  • Commit messages have short titles and reference relevant issues
  • Build passes (scons build & scons test) and unit tests address code coverage
  • Style & formatting of contributed code follows contributing guidelines
  • The pull request is ready for review

@property
def element_names(self) -> list[str]: ...
def atomic_weight(self, m: int) -> float: ...
def atomic_weight(self, m: str | bytes | float) -> float: ...
Copy link
Copy Markdown
Member

@ischoegl ischoegl Oct 29, 2025

Choose a reason for hiding this comment

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

The float really should be an int, as it refers to an index in the storage order. The float should be deprecated sooner rather than later, as it's extremely misleading.

Similar goes for bytes, which is just something where a pystr conversion was missed.

PS: It's actually interesting to see that type hinting exposes some of the more questionable argument types that we're still supporting .

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Indeed, I would have typed it as an int if it wasn't explicitly checking both int and float internally. I agree that it would be better to remove float as a valid option.

Regarding bytes, that's probably only "correct" because bytestrings are a thing, but I don't recall seeing anything which still returns bytes. It should be safe to drop them from all of the type annotations right now, and shouldn't be difficult to eliminate their remaining usage within the source code.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi guys, I haven't had time to look at the implementation here, but I do want to comment so please don't merge. In general I would prefer, and I think it makes sense from a user perspective, to have the types be stricter than what the interface actually accepts. I suppose it's not strictly correct, but this isn't C or even Go where the code won't even compile with incorrect types, so user convenience matters more to me.

With respect to the float conversion, I'm fairly sure I did that to support a specific use case that escapes me at the moment, but for example might be pulling values out of a string which are often '10.0', so int() would fail. Aside from that, changing the function's types it accepts should go through a deprecation cycle that doesn't seem worth the churn.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@bryanwweber I do agree that it would make sense to generally prefer type hints which match the intended usage, even if that is stricter than what the implementation will actually accept. We've made such changes in a few spots, such as using tuples for property setters which would technically accept any Sequence type.

I'd be fine with either rescoping this pull request to include removing most/all bytes throughout the stubs, or making a subsequent pull request to do so. This was just intended to be a super quick change when the inconsistency popped up in one of my codebases.

Copy link
Copy Markdown
Member

@ischoegl ischoegl Oct 29, 2025

Choose a reason for hiding this comment

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

@bryanwweber ... I see the convenience, but allowing a float for an index makes me shudder. I strongly recommend excluding this from being suggested by a type hint.

Similar goes for bytes ... we 'tolerate' it, but I absolutely don't want users to get ideas. The Python API uses str, full stop?

@TimothyEDawson ... either is fine by me, and I overall agree that type hints can be narrower than what is actually processed correctly. I only commented here as I realized that a float type hint was used for an index.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I strongly recommend excluding this from being suggested by a type hint.

This is exactly what I'm suggesting. Even if the underlying implementation permits a float, we don't need to add that to the type signature. And we should not change the implementation to match the type signature, i.e., we should leave the implementation as-is.

Copy link
Copy Markdown
Member

@ischoegl ischoegl Oct 30, 2025

Choose a reason for hiding this comment

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

I’m a strong 👎 on float indices, but oh well. It’s a pre-existing condition, so as long as it doesn’t make it into type hints, I can live with it.

@TimothyEDawson
Copy link
Copy Markdown
Contributor Author

I went ahead and dropped bytes and float from the type hints for various indices, leaving only str and int. A few other misc. changes in here came from Ruff formatting rules (e.g. sorting the imports and importing Mapping from collections.abc).

@codecov
Copy link
Copy Markdown

codecov bot commented Oct 30, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.47%. Comparing base (19af7f8) to head (07fcb7d).
⚠️ Report is 10 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2024      +/-   ##
==========================================
- Coverage   76.47%   76.47%   -0.01%     
==========================================
  Files         464      464              
  Lines       54938    54936       -2     
  Branches     9305     9305              
==========================================
- Hits        42014    42012       -2     
  Misses       9798     9798              
  Partials     3126     3126              

☔ View full report in Codecov by Sentry.
📢 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.

Copy link
Copy Markdown
Member

@ischoegl ischoegl left a comment

Choose a reason for hiding this comment

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

Thank you, @TimothyEDawson

I will still make a comment about the general utility of int for element_index et al.

>>> gas.species_index("CH4")
13
>>> gas.species_index(13)
13
>>> gas.species_index(12.99999999999999)  # this is why allowing float is terrible
12

As I recently looked at this code, I know why the int is allowed here (it's there to check whether the index is within bounds), but it is not something I am overall endorsing. From that perspective, it may make sense to restrict the type hint to str alone.

@ischoegl ischoegl merged commit 5eb2db6 into Cantera:main Oct 30, 2025
54 checks passed
@TimothyEDawson TimothyEDawson deleted the fix-element-index-typing branch October 30, 2025 19:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants