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

Python Metadata Validation #3502

Conversation

InsertCreativityHere
Copy link
Member

This PR switches slice2py to use the 'new' metadata validator (I added 3 months ago).

The implementation, old validation, and docs disagree about these metadata, so insight would be appreciated:
python:array.array, python:numpy.ndarray, and python:memoryview:X


The docs state that array.array and numpy.ndarray are only valid on: integral builtin types excluding strings. But:

  1. "strings" are not an "integral" type... So I don't see why we mention this 'exclusion' at all.
  2. It says for "integral" types... but we have tests using this metadata with seqs of bool, float, and double.

So... I'm guessing that the documentation is just wrong. And it should say:
... are valid on sequences of numeric built-in types and bools


For python:memoryview:X.
The docs list no restriction on what this can be applied to.
But, the old validation for memoryview re-used the logic for array and ndarray, restricting what it can be applied to.

Is this correct; that memoryview can only go on sequences of built-in numeric types and bools?
Or did we incorrectly re-use logic, and in reality, this metadata can be used with any sequence type?

@@ -2426,8 +2403,7 @@ Slice::Python::getImportFileName(const string& file, const UnitPtr& ut, const ve
void
Slice::Python::generate(const UnitPtr& un, bool all, const vector<string>& includePaths, Output& out)
{
Slice::Python::MetadataVisitor visitor;
un->visit(&visitor);
validateMetadata(un);
Copy link
Member

Choose a reason for hiding this comment

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

we should name the UnitPtr parameter unit.

Copy link
Member Author

Choose a reason for hiding this comment

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

I fixed it in these locations, but in general we have many different (and inconsistent) names for unit.
unt is my personal favorite. We just have to save that i character...

bool
Slice::Python::MetadataVisitor::visitUnitStart(const UnitPtr& unit)
void
Slice::Python::validateMetadata(const UnitPtr& u)
Copy link
Member

Choose a reason for hiding this comment

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

unit please

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

SequencePtr seq = dynamic_pointer_cast<Sequence>(type);
if (seq)
// If 'python:package' is applied to a module, it must be a top-level module.
// // Top-level modules are contained by the 'Unit'. Non-top-level modules are contained in 'Module's.
Copy link
Member

Choose a reason for hiding this comment

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

Double comment

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch! Fixed.

.validArgumentValues = {{"default", "list", "tuple"}},
.acceptedContext = MetadataApplicationContext::DefinitionsAndTypeReferences,
};
MetadataInfo unqualifiedSeqInfo = {
Copy link
Member

Choose a reason for hiding this comment

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

It would be clearer to define this variable after:

knownMetadata.emplace("python:seq", std::move(seqInfo));

Copy link
Member Author

Choose a reason for hiding this comment

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

Fine, I moved it.

@pepone
Copy link
Member

pepone commented Feb 6, 2025

... are valid on sequences of numeric built-in types and bools

Yes. I think this is correct.

Is this correct; that memoryview can only go on sequences of built-in numeric types and bools?

Yes. We should have the same restrictions

knownMetadata.emplace("python:numpy.ndarray", std::move(arrayTypeInfo));

// "python:memoryview"
MetadataInfo memoryViewInfo = {
Copy link
Member

Choose a reason for hiding this comment

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

Can we reuse the validation function from above? They all have the same restrictions.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes we can; fixed.

if (seq)
// If 'python:package' is applied to a module, it must be a top-level module.
// // Top-level modules are contained by the 'Unit'. Non-top-level modules are contained in 'Module's.
if (auto mod = dynamic_pointer_cast<Module>(p); mod && !dynamic_pointer_cast<Unit>(mod->container()))
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should add a isTopLevel in modules, I think we have checks like this in a few places.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added this function to the parser, and used it everywhere where we could use it.
I've been thinking about adding this for a while, so I already had a list of places to use it!

One day I still need to add a getTopLevelModule function somewhere as well...

@InsertCreativityHere InsertCreativityHere merged commit c7a5a9b into zeroc-ice:main Feb 10, 2025
23 checks passed
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.

3 participants