graphql-python / graphene-pydantic

Integrate GraphQL with your Pydantic models
Other
229 stars 44 forks source link

Examples don't work in Python 3.6 #11

Closed necaris closed 5 years ago

necaris commented 5 years ago

See #10 -- while the examples work fine in Python 3.7, it would be ideal if we could make them work (or document where they don't) in 3.6

dantheman39 commented 5 years ago

The issue seems to be here: https://github.com/upsidetravel/graphene-pydantic/blob/f0326a75c7c3e7b0b5dfa1aa58d0fd864da44705/examples/departments.py#L35

class DepartmentModel(pydantic.BaseModel):
    id: uuid.UUID
    name: str
    employees: T.List[T.Union[ManagerModel, EmployeeModel]]

employees is a Union of Manager and Employee, and Manager is a subclass of Employee. In 3.6, typing.py throws away direct subclasses in Unions, so in DepartmentModel.__annotations__, the employees field is listed as typing.List[__main__.EmployeeModel], and not typing.List[typing.Union[__main__.ManagerModel, __main__.EmployeeModel]] as in 3.7, so all of the introspection that we rely on has no way of knowing that employees is a Union of Manager and Employee, it just thinks it's an employee.

In the documentation for Union they explicitly note that subclasses are no longer thrown out as of python 3.7.

necaris commented 5 years ago

Closed by #12