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

[Feature Request] Support for Python 3.11 #186

Closed
4 tasks
zhiruiluo opened this issue Dec 14, 2022 · 2 comments · Fixed by #213
Closed
4 tasks

[Feature Request] Support for Python 3.11 #186

zhiruiluo opened this issue Dec 14, 2022 · 2 comments · Fixed by #213

Comments

@zhiruiluo
Copy link
Contributor

zhiruiluo commented Dec 14, 2022

Feature requested

Is your feature request related to a problem? Please describe.
Add support for Python 3.11.
Python 3.11.1 is released at Dec. 6 2022.
ref: https://www.python.org/downloads/release/python-3111/

Python 3.11 support graph for the 360 most popular Python packages! https://pyreadiness.org/3.11/

Highlight:

  • Python 3.11 is between 10-60% faster than Python 3.10. On average, we measured a 1.25x speedup on the standard benchmark suite.

Motivation

Python 3.11 is fast! And we enjoy speedup 😊!

Known issue

  • Change field default mutability check, allowing only defaults which are hashable instead of any object which is not an instance of dict, list or set. (Contributed by Eric V. Smith in bpo-44674.) [ref]
  • dataclass decorator requires nested dataclasses to use default_factory or make nested dataclasses hashable
  • Many test cases using mutable default will fail
  • get_help_text in TestSetup fail for nested dataclasses using default_factory
  • test_mutable_field in test_utils.py will fail due to default_factory initialize a new object for the shared A everytime.
  • examples using mutable default will fail in Python 3.11
  • simple_parsing.decorator main will need to be modified.
  • field_wrapper default value might be set by default_factory.

Error Example:

from dataclasses import dataclass

@dataclass
class A():
    arg_str: str = 'test'
    arg_int: int = 1
    arg_float: float = 0.5

@dataclass
class B():
    a: A = A()

def test_dataclass():
    B()

output:

ERROR test/python311/test_dataclass.py - ValueError: mutable default <class 'test.python311.test_dataclass.A'> for field a is not allowed: use default_factory

Passed Example:

from dataclasses import dataclass

@dataclass(eq=True,frozen=True)
class A():
    arg_str: str = 'test'
    arg_int: int = 1
    arg_float: float = 0.5

@dataclass
class B():
    a: A = A()

def test_dataclass():
    B()

Describe the solution you'd like

  • Add test for Python 3.11 to CI
  • Modify test cases and examples to comply the default mutable check
  • Add docs for different usages in Python==3.11 and Python<=3.10
  • Identify and fix related bugs

Describe alternatives you've considered

Additional context

@zhiruiluo
Copy link
Contributor Author

Possible solution

We can pass dataclasses to the default_factory argument of field.

from dataclasses import dataclass, field

@dataclass()
class A():
    arg_str: str = 'test'
    arg_int: int = 1
    arg_float: float = 0.5

@dataclass
class B():
    # using lambda to initialize a user specified dataclass object
    a: A = field(default_factory=lambda: A(arg_str='hi'))

@dataclass
class C():
    # or pass a dataclass class direct to default_factory 
    a: A = field(default_factory=A)

def test_dataclass():
    B()
    C()

@lebrice
Copy link
Owner

lebrice commented Jan 5, 2023

This makes total sense. Will look into this as soon as I can. Thanks @zhiruiluo ! :)

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 a pull request may close this issue.

2 participants