-
Notifications
You must be signed in to change notification settings - Fork 503
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
feat(bindings/python): generate python operator constructor types #5457
Conversation
eaadb04
to
34c2d63
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly LGTM, but I think we should revist the _bool
and _int
type aliases later (due to the use of Operator::from_iter
that only accepts a iterator of (String, String)
), IMHO at least the _int
type should accepts int
.
This comment was marked as off-topic.
This comment was marked as off-topic.
pyo3 doesn't do implicit type conversion here:
|
I think we can fix it from the core side. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, we just generate the .pyi
code here. Would it work better if we had classes or types like S3Config
on the Python side? This way, we could include more information, such as comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other concern here is that I would prefer not to maintain .pyi
files ourselves in the future. We should use tools like a stub file generator, as mentioned in PyO3/pyo3#510, such as https://crates.io/crates/pyo3-stub-gen (raising by @trim21).
Instead, we can simply keep the struct in Python with comments and let other tools generate the .pyi
files for us.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it work better if we had classes or types like
S3Config
on the Python side? This way, we could include more information, such as comments.
I think having Python config types is better, but it can be done later.
Use pyo3-stub-gen would solve typing issue in python side (assuming it can satisfy our needs, I've never tried it so I'm not sure), not sure about other bindings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we may need to rewrite some rust code to make it work with pyo3-stub-gen
For me, the PR mostly LGTM, but some idea
|
We do not need to drop py<3.12 to use this feature, pyi file is not runtime code, so even in old python as long as type checker support pep692, it's still fine to use it. (I'm not sure how old version type checker will behave if it doesn't support pep692) For example, we generate XConfig to from typing import NotRequired, TypeAlias
from typing_extensions import TypedDict
# `true`/`false`` in any case, for example, `true`/`True`/`TRUE` `false`/`False`/`FALSE`
_bool: TypeAlias = str
# a str represent a int, for example, `"10"`/`"0"`
_int: TypeAlias = str
# a human readable duration string
# see https://docs.rs/humantime/latest/humantime/fn.parse_duration.html
# for more details
_duration: TypeAlias = str
# A "," separated string, for example `"127.0.0.1:1,127.0.0.1:2"`
_strings: TypeAlias = str
class S3Config(TypedDict):
bucket: str
root: NotRequired[str]
enable_versioning: NotRequired[_bool]
endpoint: NotRequired[str]
region: NotRequired[str]
access_key_id: NotRequired[str]
secret_access_key: NotRequired[str]
session_token: NotRequired[str]
role_arn: NotRequired[str]
external_id: NotRequired[str]
role_session_name: NotRequired[str]
disable_config_load: NotRequired[_bool]
disable_ec2_metadata: NotRequired[_bool]
allow_anonymous: NotRequired[_bool]
server_side_encryption: NotRequired[str]
server_side_encryption_aws_kms_key_id: NotRequired[str]
server_side_encryption_customer_algorithm: NotRequired[str]
server_side_encryption_customer_key: NotRequired[str]
server_side_encryption_customer_key_md5: NotRequired[str]
default_storage_class: NotRequired[str]
enable_virtual_host_style: NotRequired[_bool]
# deprecated: Please use `delete_max_size` instead of `batch_max_operations`
batch_max_operations: NotRequired[_int]
delete_max_size: NotRequired[_int]
disable_stat_with_override: NotRequired[_bool]
checksum_algorithm: NotRequired[str]
disable_write_with_if_match: NotRequired[_bool] from typing import overload, Literal, Unpack
from opendal.config import S3Config
class _Base:
"""this is not a real base class but typing mixin,
The services list here is support by opendal pypi wheel.
"""
@overload
def __init__(
self,
scheme: Literal["s3"],
**kwargs: Unpack[S3Config],
) -> None: ...
@overload
def __init__(self, scheme: str, **kwargs: str) -> None: ... |
I guess rust code is fine for now? Still need to resolve what kind of typing we want to generate. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, @trim21 this will be large task, would you like to create a tracking issue for the progress? |
It's not a blocking issue, this improves the status quo of typing for python binding already. The rest can be followed up later. Thanks! |
Which issue does this PR close?
Closes #.
Rationale for this change
What changes are included in this PR?
A python pyi generator
This PR requires #5463
a problem I found:
FsConfig.root
isOption<String>
which is actually required.opendal/core/src/services/fs/config.rs
Lines 27 to 29 in 22d2cef
opendal/core/src/services/fs/backend.rs
Lines 84 to 90 in 22d2cef
Are there any user-facing changes?
Yes, add python typing.