Conversation
| python_to_model = { | ||
| str: inmanta_type.String(), | ||
| float: inmanta_type.Float(), | ||
| numbers.Number: inmanta_type.Number(), |
There was a problem hiding this comment.
Do we want to support this, given that the number type has been deprecated?
There was a problem hiding this comment.
Deprecated, but not gone.
I have no strong opinion on this
There was a problem hiding this comment.
After giving it some more thought, me neither. At some level I'd prefer not to integrate deprecated types into new features, but if it's as simple as this I guess it doesn't hurt too much either. The only thing is perhaps that we have the opportunity to make a clean cut here, to be more visible than the current deprecation warning.
| @@ -267,11 +345,13 @@ def resolve_type(self, plugin: "Plugin", resolver: Namespace) -> inmanta_type.Ty | |||
| return self._resolved_type | |||
|
|
|||
| if not isinstance(self.type_expression, str): | |||
There was a problem hiding this comment.
Ok, so for iso 8.1 we also aim to evaluate strings here, right?
There was a problem hiding this comment.
I had understood that every string is considered to be an inmanta type
There was a problem hiding this comment.
I think I would prefer to also support python type strings, because that's what you would expect when writing Python. But that doesn't have to be now necessarily. What I had in mind was to
- try to evaluate the annotation as a python type expression (converting any strings, even nested ones, to types)
- convert python type expression to dsl type expression
If 1 fails -> fall back to dsl type parsing.
But I don't feel too strongly about this.
There was a problem hiding this comment.
I don't want that. That would create the same type of problem that yaml has (is 00:00:00:00 a number?), that parsing outcome depends on context you (often) can't know. if a type 'string' is somehow in scope, the typing changes
There was a problem hiding this comment.
Perhaps a key element in my reasoning is that I approach it from the idea that the pure dsl annotations would eventually disappear, leaving only the typing.Annotated when you really need them. With that reasoning, the Python parsing would always be the primary, and the DSL parsing would become only a backwards compatibility fallback.
But if that's not how you see it evolve, then perhaps I agree with you, given your argument above. I still think it can have unintuitive aspects of its own, but the typing.Annotated should give us a way to work around any we encounter to give ourselves time to discover where we really want to go.
| str: inmanta_type.String(), | ||
| float: inmanta_type.Float(), | ||
| numbers.Number: inmanta_type.Number(), | ||
| int: inmanta_type.Integer(), | ||
| bool: inmanta_type.Bool(), | ||
| dict: inmanta_type.LiteralDict(), | ||
| list: inmanta_type.LiteralList(), |
There was a problem hiding this comment.
I'm actually not sure about hardcoding this mapping to the type objects. I think I'd prefer to use the existing inmanta.ast.type.TYPES mapping, so that if we ever change something there it is reflected in both flows. Then this mapping here would simply be a mapping to the model types as represented in the model, e.g. str -> "string".
There was a problem hiding this comment.
Upon closer consideration, what I had here was even wrong.
inmanta.ast.type.TYPES is the internal attribute types, which are subtly different from what is here, mapping it twice won't be correct / consistent with what existed already.
There was a problem hiding this comment.
I'm not following with this. From your comment I understood that you agreed we should use inmanta.ast.type.TYPES, but from the implementation I think I misunderstood? Why should we not use it?
There was a problem hiding this comment.
I don't think we should use inmanta.ast.type.TYPES because:
- I consider the type object the interface, so we should decouple on that
inmanta.ast.type.TYPESmaps attribute type string to inmanta types, but this is subtly different from the type mapping at the plugin boundary. E.g."dict": LiteralDict(),Vsdict: inmanta_type.TypedDict(inmanta_type.Type()),And this is a pre-existing condition
| if len(other_types) == 1: | ||
| return inmanta_type.NullableType(to_dsl_type(other_types[0])) | ||
| # TODO: optional unions | ||
| return inmanta_type.AnyType() |
There was a problem hiding this comment.
So we create a follow-up ticket to fix all these any types by 8.1? They could cause some very unintuitive behavior I think.
There was a problem hiding this comment.
Then again, aren't these relatively easy to handle?
There was a problem hiding this comment.
I will ticketize and we'll see when we get them.
(e.g. the unions could be easy, but then again, we don't use unions anywhere yet, so they could also be very difficult)
There was a problem hiding this comment.
Can you mark the tickets for the 8.1 release? I don't want to keep all these Any's for longer than required, so I'd like to address it (on iso8) while we can still consider it a non-breaking change (because it's in there now but not documented).
|
|
||
| if typing_inspect.is_generic_type(python_type): | ||
| origin = typing.get_origin(python_type) | ||
| if origin is Sequence: |
There was a problem hiding this comment.
From the design document: "Collection and Sequence as return types will require minor changes to DynamicProxy.unwrap, which now contains isinstance(item, list)" The same might go for Mapping, I haven't checked.
There was a problem hiding this comment.
I think this is still missing.
Co-authored-by: Bart Vanbrabant <bart.vanbrabant@inmanta.com>
…into issue/native_types
Co-authored-by: Sander Van Balen <git@sandervanbalen.be>
…into issue/native_types
| Mapping: inmanta_type.TypedDict(inmanta_type.Type()), | ||
| collections.abc.Mapping: inmanta_type.TypedDict(inmanta_type.Type()), |
There was a problem hiding this comment.
Something weird is going on here. Mapping should be from collections.abc. I think it's good to support both, but then the qualification must be the other way around (or both qualified if you prefer). Because it's very likely that the import will be updated at some point.
| if not args: | ||
| return inmanta_type.List() | ||
| return inmanta_type.TypedList(to_dsl_type(args[0])) | ||
| if origin in [collections.abc.Sequence, list, typing.Sequence]: |
There was a problem hiding this comment.
Collection also contains Mapping and Set, so I think that is a bit too wide?
There was a problem hiding this comment.
But we're not accepting subclasses anymore, only those exact annotations. A list is a collection, so that would be acceptable for input args. And we can easily construct lists from it so it's acceptable as return type.
But I'm not too attached to it, it's not that common.
| return inmanta_type.TypedDict(to_dsl_type(args[1])) | ||
| return inmanta_type.TypedDict(to_dsl_type(args[1])) | ||
| else: | ||
| raise TypingException(None, f"invalid type {python_type}, dictionary types should be Mapping or dict") |
There was a problem hiding this comment.
Nice. I hadn't considered this, but it's good to have a clear message!
One suggestion
| raise TypingException(None, f"invalid type {python_type}, dictionary types should be Mapping or dict") | |
| raise TypingException(None, f"invalid type {python_type}, dictionary types should be Mapping (preferred) or dict") |
There was a problem hiding this comment.
I think the order implies preference?
There was a problem hiding this comment.
I guess. I mostly meant to say that I think we should discourage the dict, even if we accept it, because it's simply not accurate.
There was a problem hiding this comment.
We could even not mention the dict, but it is the most familiar to most people
|
@wouterdb FYI, there are still some threads that I consider relevant. You may have just skipped over them for now, but if you missed them this should bring them to your attention. I resolved all threads that I consider no longer relevant. Some of the others are only really relevant for follow-up tickets (e.g. whether or not we want to support stringified Python type expressions), others are decisions that need to be made (e.g. |
sanderr
left a comment
There was a problem hiding this comment.
Looks like we're starting to align on a middle ground. One final note on something that I'd prefer to see differently, but I can let it slide if you disagree.
| if python_type in python_to_model: | ||
| return python_to_model[python_type] | ||
|
|
||
| return inmanta_type.Type() |
There was a problem hiding this comment.
I'm not a fan of this fallback to Any. This may easily give the user the false impression that they're getting type validation, while that is not actually the case.
There was a problem hiding this comment.
See my proposed extensions on slack
|
Processing this pull request |
|
Failed to merge changes into iso7 due to merge conflict. Please open a pull request for these branches separately by cherry-picking the commit that was made on the branch master (git cherry-pick 881d1d5). |
|
Merged into branches master in 881d1d5 |
# Description Basic native python types For the documentation, I would add a separate ticket # Self Check: Strike through any lines that are not applicable (`~~line~~`) then check the box - [x] Attached issue to pull request - [x] Changelog entry - [x] Type annotations are present - [x] Code is clear and sufficiently documented - [x] No (preventable) type errors (check using make mypy or make mypy-diff) - [x] Sufficient test cases (reproduces the bug/tests the requested feature) - [x] Correct, in line with design - [ ] End user documentation is included or an issue is created for end-user documentation (add ref to issue here: ) - [ ] If this PR fixes a race condition in the test suite, also push the fix to the relevant stable branche(s) (see [test-fixes](https://internal.inmanta.com/development/core/tasks/build-master.html#test-fixes) for more info)
|
Not closing this pull request due to previously commented issues for some of the destination branches. Please open a separate pull request for those branches by cherry-picking the relevant commit. You can safely close this pull request and delete the source branch. |
|
This branch was not deleted as it seems to still be in use. |
Basic native python types For the documentation, I would add a separate ticket Strike through any lines that are not applicable (`~~line~~`) then check the box - [x] Attached issue to pull request - [x] Changelog entry - [x] Type annotations are present - [x] Code is clear and sufficiently documented - [x] No (preventable) type errors (check using make mypy or make mypy-diff) - [x] Sufficient test cases (reproduces the bug/tests the requested feature) - [x] Correct, in line with design - [ ] End user documentation is included or an issue is created for end-user documentation (add ref to issue here: ) - [ ] If this PR fixes a race condition in the test suite, also push the fix to the relevant stable branche(s) (see [test-fixes](https://internal.inmanta.com/development/core/tasks/build-master.html#test-fixes) for more info)
Description
Basic native python types
For the documentation, I would add a separate ticket
Self Check:
Strike through any lines that are not applicable (
~~line~~) then check the box