Skip to content

[RFC] Rust name or Python name? #3284

@youknowone

Description

@youknowone

Summary

This is only about python objects like #[pymodule], #[pyclass], #[pyfunction], #[pymethod] and #[pyproperty].
Sometimes python names are not fit for rust naming convention.

  1. Though we can specify the name with (name = "") attributes, we also need to decide how we give names for its rust side API.
  2. There are also python names conflict to rust keywords. They also need a special rule because we normally cannot name them same in Rust.

Detailed Explanation

List of unmatching names.

  1. Python type int is type PyInt in Rust API.
  2. Python function posix.WIFSIGNALED is posix::wifsignaled in Rust.
  3. Python type _struct.error is PyTypeRef object pystruct::error_type in Rust.
  4. Python type _weakref.ReferenceType is a function returning PyTypeRef _weakref::reference_type in Rust.
  5. Python variable sys.api_version is const sys::API_VERSION in Rust.
  6. Python variable dis.COMPILER_FLAG_NAMES is a function returning PyDictRef dis::compiler_flag_names in Rust.

We must handle types differently.
For example, we can expose &PyTypeRef for builtins::int() but PyInt through builtins::PyInt.

We can choose one of these kind of options for other names:

  1. Strictly follow Python names unless it is impossible.
  • The names will not conflict.
  • Some names will be r# prefixed in Rust. They would need human-friendly aliases.
  1. Follow Python names as much as possible but with exceptions by its paired Rust type.
  • For example, _weakref.ReferenceType and int are both type in Python but PyTypeRef instance and PyInt struct for each in Rust. Similar for sys.api_version and dis.COMPILER_FLAG_NAMES
  1. Export a python-name aliased module separately. This can be generated by derive macros.
  • For this choice, we can optionally suggest more consistency.
    1. We can expose variable always through a function call. A function to return a const will be generated.
    2. We can attach emitted vm parameter again to functions.
  • Still types will be problem.

List of keyword conflict.

  1. struct module.
  • The exact python name is _struct
  1. mod operator.
  • The exact python name is __mod__
  1. match function.
  2. type type.
  3. enum module.

List of confusing non-keyword names.

  1. bool types
  2. str types
  3. Iterator for python and rust

Example of solutions

  1. Always keep python name using r#. But allow alias.
  2. Add additional _ suffix for conflicting type. match will be match_. But allow r# alias.
  3. Add py prefix like current pybool, pytype, pystruct.

Drawbacks, Rationale, and Alternatives

Unresolved Questions

Everything. Any dicision will give concrete idea about public API design.

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFCRequest for comments

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions