Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix _add_enum_value_python_name() to respect explicitly set value names #2073

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

ni-jfitzger
Copy link
Collaborator

@ni-jfitzger ni-jfitzger commented Jan 26, 2025

  • This contribution adheres to CONTRIBUTING.md.
  • [ ] I've updated CHANGELOG.md if applicable.
  • I've added tests applicable for this pull request

What does this Pull Request accomplish?

The python_name metadata key is supposed to allow us to explicitly set the name that will be used by the Python API. For enum values, this was not being respected.

This change fixes that and adds additional unit test coverage for enum value expansion.

As part of this, we have to temporarily override some enum value metadata. Followup item: #2072

List issues fixed by this Pull Request below, if any.

What testing has been done?

  • Unit tests pass
  • Codegen has run and API is unchanged
  • PR Checks

'description': 'Custom'
},
'name': 'NIDMM_VAL_TEMP_THERMISTOR_CUSTOM',
'python_name': 'CUSTOM',
Copy link
Collaborator Author

@ni-jfitzger ni-jfitzger Jan 26, 2025

Choose a reason for hiding this comment

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

Strangely, (with the codegen helper changes) if I set python_name for all the values except this one, this one came back as TEMP. I'm not sure why it gets cut off like that, but didn't feel it was worth further investigation.

Copy link
Member

Choose a reason for hiding this comment

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

Does that mean that if a client wants to explicitly provide the python name for a subset of enum values, we don't know what will be the expanded value here?

I feel like it's worth getting to the bottom of, and having a test case for.

Copy link
Collaborator Author

@ni-jfitzger ni-jfitzger Jan 26, 2025

Choose a reason for hiding this comment

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

It was almost certainly due to common suffix removal.

The behavior was a little bit hard to follow, so I've rewritten my change and instead of using a boolean key for tracking whether to touch something, I'll have us do all of the processsing with a temporary key, '_python_name'.

I've also added another unit test enum related to this. There's a peculiar behavior with common prefix and common suffix removal when you only have one value to calculate the prefix or suffix from. It calculates the prefix or suffix as the entire string and we then remove almost that entire string (upto and including the first/last underscore). I setting prefix and suffix to '', instead, when there's less than 2 values to calculate the commmon prefix or suffix from, but (upsetting as it is) we have one or two enums with a single enum value that actually make use of this behavior. I can still change it if you want, though. It seems like a footgun.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

With the latest change, we no longer need to set 'python_name': 'CUSTOM', but this behavior still seems like a footgun.

Copy link
Member

Choose a reason for hiding this comment

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

If the behavior observed when only a subset of the enum values has python_name explicitly in metadata is hard to describle / footgun, we could consider disallowing this altogether and forcing clients to specify none or all. I think the case is rare enough that there would be very few cases of this.

Counterpoint is that bad expansion of a subset of enum values should be fairly apparent in generated code diffs.

What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's easy to describe at this point. Everything up through the last underscore is trimmed. This is the same behavior we would see if the values that use python_name didn't exist.

You're correct that is very rare and even rarer is the case where we have an enum with only one value and the one value has multiple words and that enum is used somewhere so we don't filter it out in codegen. We only seem to have a couple of those and the current value names seem okay.

I think it's fine to leave this change as is. We should be able to catch any issues in review.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

we could consider disallowing this altogether and forcing clients to specify none or all

Yeah, that's another option. Wouldn't be too difficult to do, though I'm not sure how easy it is to test.

@codecov-commenter
Copy link

codecov-commenter commented Jan 26, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 75.00000% with 4 lines in your changes missing coverage. Please review.

Project coverage is 91.37%. Comparing base (afd791e) to head (701c364).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
build/helper/metadata_add_all.py 75.00% 4 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2073      +/-   ##
==========================================
+ Coverage   91.33%   91.37%   +0.03%     
==========================================
  Files          66       66              
  Lines       16274    16283       +9     
==========================================
+ Hits        14864    14878      +14     
+ Misses       1410     1405       -5     
Flag Coverage Δ
codegenunittests 85.28% <75.00%> (+0.32%) ⬆️
nidcpowersystemtests 94.59% <ø> (+0.04%) ⬆️
nidcpowerunittests 89.53% <ø> (ø)
nidigitalsystemtests 92.20% <ø> (ø)
nidigitalunittests 68.45% <ø> (ø)
nidmmsystemtests 92.72% <ø> (ø)
nifakeunittests 87.24% <ø> (ø)
nifgensystemtests 94.86% <ø> (ø)
nimodinstsystemtests 73.85% <ø> (ø)
nimodinstunittests 94.20% <ø> (ø)
niscopesystemtests 92.94% <ø> (ø)
niscopeunittests 43.20% <ø> (ø)
nisesystemtests 91.50% <ø> (ø)
niswitchsystemtests 82.03% <ø> (ø)
nitclksystemtests 94.87% <ø> (ø)
nitclkunittests 98.26% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
build/helper/metadata_add_all.py 82.06% <75.00%> (+1.31%) ⬆️

... and 1 file with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update afd791e...701c364. Read the comment docs.

build/helper/metadata_add_all.py Outdated Show resolved Hide resolved
assert v['python_name'].startswith(prefix), '{} does not start with {}'.format(v['name'], prefix)
v['prefix'] = prefix
v['python_name'] = v['python_name'].replace(prefix, '')

# Now we need to look for common suffixes
# Using the slow method of reversing a string for readability
rev_names = [''.join(reversed(v['python_name'])) for v in enum_info['values']]
# We do not include hardcoded python names when looking for common suffixes
Copy link
Member

Choose a reason for hiding this comment

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

Here you use hardcoded as a synonym to user_set. Reduce ambiguity by using one term to refer to one concept everywhere.

Copy link
Member

Choose a reason for hiding this comment

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

FWIW I like the word "explicit" for this. It means (in my mind) that it's there written in metadata.
"User set" is a bit ambiguous because it's not super clear who the user is.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've stopped using the word hardcoded and made use of the word explicit, instead, as part of my rewrite.
I eliminated the user_set_python_name key. We'll do processing on the temporary key, '_python_name', instead, so we won't need a special key to track whether we should expand the name.

'description': 'Custom'
},
'name': 'NIDMM_VAL_TEMP_THERMISTOR_CUSTOM',
'python_name': 'CUSTOM',
Copy link
Member

Choose a reason for hiding this comment

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

Does that mean that if a client wants to explicitly provide the python name for a subset of enum values, we don't know what will be the expanded value here?

I feel like it's worth getting to the bottom of, and having a test case for.

… one value.

There are existing enums with only one value that rely on this removal, as weird and questionable as that seems to me.
@ni-jfitzger ni-jfitzger changed the title Fix _add_enum_value_python_name() to respect hardcoded value names Fix _add_enum_value_python_name() to respect explicitly set value names Jan 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants