Skip to content

Generic types (as Dict) without type parameters result in an error #550

@zbarry

Description

@zbarry

Bug

For bugs/questions:

  • OS: CentOS 7
  • Python version: 3.7.2
  • Pydantic version: 0.26

Moving to separate issue here from #545

This error happens when you create a subclass of BaseModel which specifies a field of type Dict which does not specify mapping types, i.e.:

from pydantic import BaseModel
from typing import Dict, Any

class SubclassThatWorks(BaseModel):
    asdf: int
    fdsa: Dict[Any, Any]

class SubclassThatFails(BaseModel):
    asdf: int
    fdsa: Dict

with the error message being:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/scratch/miniconda3/envs/jupyter/lib/python3.7/site-packages/pydantic/validators.py in find_validators(type_, arbitrary_types_allowed)
    385         try:
--> 386             if issubclass(type_, val_type):
    387                 return validators

TypeError: issubclass() arg 1 must be a class

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
<ipython-input-5-c7b931439a60> in <module>
----> 1 class Subclass(BaseModel):
      2     asdf: int
      3     fdsa: Dict

/scratch/miniconda3/envs/jupyter/lib/python3.7/site-packages/pydantic/main.py in __new__(mcs, name, bases, namespace)
    180                     annotation=ann_type,
    181                     class_validators=vg.get_validators(ann_name),
--> 182                     config=config,
    183                 )
    184 

/scratch/miniconda3/envs/jupyter/lib/python3.7/site-packages/pydantic/fields.py in infer(cls, name, value, annotation, class_validators, config)
    136             required=required,
    137             model_config=config,
--> 138             schema=schema,
    139         )
    140 

/scratch/miniconda3/envs/jupyter/lib/python3.7/site-packages/pydantic/fields.py in __init__(self, name, type_, class_validators, model_config, default, required, alias, schema)
    105         self.parse_json: bool = False
    106         self.shape: Shape = Shape.SINGLETON
--> 107         self.prepare()
    108 
    109     @classmethod

/scratch/miniconda3/envs/jupyter/lib/python3.7/site-packages/pydantic/fields.py in prepare(self)
    170             self.allow_none = True
    171 
--> 172         self._populate_sub_fields()
    173         self._populate_validators()
    174 

/scratch/miniconda3/envs/jupyter/lib/python3.7/site-packages/pydantic/fields.py in _populate_sub_fields(self)
    221             assert issubclass(origin, Mapping)
    222             self.key_field = self._create_sub_type(
--> 223                 self.type_.__args__[0], 'key_' + self.name, for_keys=True  # type: ignore
    224             )
    225             self.type_ = self.type_.__args__[1]  # type: ignore

/scratch/miniconda3/envs/jupyter/lib/python3.7/site-packages/pydantic/fields.py in _create_sub_type(self, type_, name, for_keys)
    235             name=name,
    236             class_validators=None if for_keys else {k: v for k, v in self.class_validators.items() if not v.whole},
--> 237             model_config=self.model_config,
    238         )
    239 

/scratch/miniconda3/envs/jupyter/lib/python3.7/site-packages/pydantic/fields.py in __init__(self, name, type_, class_validators, model_config, default, required, alias, schema)
    105         self.parse_json: bool = False
    106         self.shape: Shape = Shape.SINGLETON
--> 107         self.prepare()
    108 
    109     @classmethod

/scratch/miniconda3/envs/jupyter/lib/python3.7/site-packages/pydantic/fields.py in prepare(self)
    171 
    172         self._populate_sub_fields()
--> 173         self._populate_validators()
    174 
    175     def _populate_sub_fields(self) -> None:  # noqa: C901 (ignore complexity)

/scratch/miniconda3/envs/jupyter/lib/python3.7/site-packages/pydantic/fields.py in _populate_validators(self)
    253                     get_validators()
    254                     if get_validators
--> 255                     else find_validators(self.type_, self.model_config.arbitrary_types_allowed)
    256                 ),
    257                 self.schema is not None and self.schema.const and constant_validator,

/scratch/miniconda3/envs/jupyter/lib/python3.7/site-packages/pydantic/validators.py in find_validators(type_, arbitrary_types_allowed)
    387                 return validators
    388         except TypeError as e:
--> 389             raise RuntimeError(f'error checking inheritance of {type_!r} (type: {display_as_type(type_)})') from e
    390 
    391     if arbitrary_types_allowed:

RuntimeError: error checking inheritance of ~KT (type: KT)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.Xhelp wantedPull Request welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions