Fixed optional typing on non-serializable types
Describe changes
This code:
from typing import Optional
from zenml import step, pipeline
from sklearn.neighbors import NearestNeighbors
import numpy as np
@step
def one():
X = np.array([[1, 1], [2, 2]])
return NearestNeighbors().fit(X)
@step
def two(a: Optional[int] = None, nn: Optional[NearestNeighbors] = None):
pass
@pipeline()
def simple_pipeline():
nn = one()
two(nn=None, a=None)
simple_pipeline()
Was failing with the following error message:
│ 404 │ def _unknown_type_schema(self, obj: Any) -> CoreSchema: │
│ ❱ 405 │ │ raise PydanticSchemaGenerationError( │
│ 406 │ │ │ f'Unable to generate pydantic-core schema for {obj!r}. ' │
│ 407 │ │ │ 'Set `arbitrary_types_allowed=True` in the model_config to ignore this error │
│ 408 │ │ │ ' or implement `__get_pydantic_core_schema__` on your type to fully support │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'sklearn.neighbors._unsupervised.NearestNeighbors'>. Set `arbitrary_types_allowed=True` in the model_config to ignore this error or
implement `__get_pydantic_core_schema__` on your type to fully support it.
If you got this error by calling handler(<some type>) within `__get_pydantic_core_schema__` then you likely need to call `handler.generate_schema(<some type>)` since we do not call `__get_pydantic_core_schema__` on
`<some type>` otherwise to avoid infinite recursion.
For further information visit https://errors.pydantic.dev/2.7/u/schema-for-unknown-type
def two(a: Optional[int] = None, nn: Optional[NearestNeighbors] = None):
While primitive types allowed for optional typing, non-serializable types like NearestNeighbors lead to pydantic errors, as the utility functions were only checking serializable types (as the assumption probably was that at this point we are handling only parameters, not artifacts).
Pre-requisites
Please ensure you have done the following:
- [ ] I have read the CONTRIBUTING.md document.
- [ ] If my change requires a change to docs, I have updated the documentation accordingly.
- [ ] I have added tests to cover my changes.
- [ ] I have based my new branch on
developand the open PR is targetingdevelop. If your branch wasn't based on develop read Contribution guide on rebasing branch to develop. - [ ] If my changes require changes to the dashboard, these changes are communicated/requested.
Types of changes
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] Other (add details above)
[!IMPORTANT]
Review skipped
Auto reviews are disabled on this repository.
Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?
Tips
Chat
There are 3 ways to chat with CodeRabbit:
- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
I pushed a fix in commit <commit_id>.Generate unit testing code for this file.Open a follow-up GitHub issue for this discussion.
- Files and specific lines of code (under the "Files changed" tab): Tag
@coderabbitaiin a new review comment at the desired location with your query. Examples:@coderabbitai generate unit testing code for this file.@coderabbitai modularize this function.
- PR comments: Tag
@coderabbitaiin a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:@coderabbitai generate interesting stats about this repository and render them as a table.@coderabbitai show all the console.log statements in this repository.@coderabbitai read src/utils.ts and generate unit testing code.@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.@coderabbitai help me debug CodeRabbit configuration file.
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.
CodeRabbit Commands (invoked as PR comments)
@coderabbitai pauseto pause the reviews on a PR.@coderabbitai resumeto resume the paused reviews.@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository.@coderabbitai full reviewto do a full review from scratch and review all the files again.@coderabbitai summaryto regenerate the summary of the PR.@coderabbitai resolveresolve all the CodeRabbit review comments.@coderabbitai configurationto show the current CodeRabbit configuration for the repository.@coderabbitai helpto get help.
Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
Documentation and Community
- Visit our Documentation for detailed information on how to use CodeRabbit.
- Join our Discord Community to get help, request features, and share feedback.
- Follow us on X/Twitter for updates and announcements.
Also, this really screams for a test case
already had some, are any obvious ones missing?
I'm still not entirely sure how to handle this. On the one hand, it would be nice to have your code work as is. On the other hand, if someone now we're to switch the default value to something Non-Null, we would fail and say a parameter has to be JSON-serializable, which also seems weird.
In case we want this though, I think we could achieve this by allowing arbitrary types in case the value is None:
def _validate_parameter_input_value(
self, parameter: inspect.Parameter, value: Any
) -> None:
arbitrary_types_allowed = value is None
config_dict = ConfigDict(arbitrary_types_allowed=arbitrary_types_allowed)
...
Closing in favor of #3215