Sai Naveen
Sai Naveen

Reputation: 39

DataClassWizard after upgrading to python3.11 is not working as expected

Consider this following code:
@dataclass
class Subjects(JSONWizard):
    subjectName: str =None
    subjectExpert: str = None
    subjectScore: float = None
@dataclass
class Student(JSONWizard):
    studentName: str
    studentId: str
    subjects: list[Subjects] = None
    teachers: list = None
    paidFee: bool = None
    gender: str = None
    attendancePercentage: float = None
    
sample_record = {
    "studentName":"Naveen", "studentId":"18B91A05L2","Subjects":[{"subjectName":"Telugu","subjectExpert":"Lingeswar","subjectScore":23.5}],
    "teachers":["BhaskarRao","LaxmiKumari","Louis"],"paidFee":True,"gender":"M", "attendancePercentage":98.2
}

dataclass_object: Student = Student.from_dict(sample_record)
print(dataclass_object.studentName)

I have ran it in python3.9 and its working fine but in python3.11 I am facing this error: dataclass_wizard.errors.ParseError: Failure parsing field None in class None. Expected a type Any, got NoneType. value: None error: Provided type is not currently supported. unsupported_type: typing.Any Can someone help me in understanding whats wrong here!

Upvotes: 3

Views: 478

Answers (1)

Robino
Robino

Reputation: 4929

In the "typing" section of the release notes for python 3.11 it says:

Allow subclassing of typing.Any. This is useful for avoiding type checker errors related to highly dynamic class, such as mocks. (Contributed by Shantanu Jain in gh-91154.)

This means that the following code returns True in 3.11, whereas in 3.10 it returns False:

from typing import Any
isinstance(Any, type)  # ***

The dataclass_wizard attemps to discover the base_type of each member through a chain of elif statements:

if isinstance(base_type, type):
    ...
elif base_type is Any:
    ...  # 3.10 constructs the None here.
...

Since the first if statement no longer evaluates to False, the base_type is never discovered and the dataclass_wizard fails.

You can easily fix this fresh bug in dataclass_wizard by reversing the first two if statements:

if base_type is Any:
    ...  # 3.10 & 3.11 construct the None here.
elif isinstance(base_type, type):
    ...  
...

Upvotes: 2

Related Questions