Skip to content

Commit

Permalink
Fix xfailed test for generic model signatures (#7658)
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb committed Sep 27, 2023
1 parent ea9aa13 commit 1377c04
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 18 deletions.
10 changes: 2 additions & 8 deletions pydantic/_internal/_core_utils.py
Expand Up @@ -169,17 +169,10 @@ def _is_schema_valid(s: core_schema.CoreSchema, recurse: Recurse) -> core_schema
metadata = s['metadata']
if HAS_INVALID_SCHEMAS_METADATA_KEY in metadata:
invalid = metadata[HAS_INVALID_SCHEMAS_METADATA_KEY]
if invalid is True:
invalid = True
return s
return recurse(s, _is_schema_valid)

walk_core_schema(schema, _is_schema_valid)
if 'metadata' in schema:
metadata = schema['metadata']
metadata[HAS_INVALID_SCHEMAS_METADATA_KEY] = invalid
else:
schema['metadata'] = {HAS_INVALID_SCHEMAS_METADATA_KEY: invalid}
return invalid


Expand Down Expand Up @@ -444,7 +437,8 @@ def collect_refs(s: core_schema.CoreSchema, recurse: Recurse) -> core_schema.Cor
for definition in s['definitions']:
ref = get_ref(definition)
assert ref is not None
definitions[ref] = definition
if ref not in definitions:
definitions[ref] = definition
recurse(definition, collect_refs)
return recurse(s['schema'], collect_refs)
else:
Expand Down
7 changes: 0 additions & 7 deletions pydantic/_internal/_generate_schema.py
Expand Up @@ -50,7 +50,6 @@
build_metadata_dict,
)
from ._core_utils import (
HAS_INVALID_SCHEMAS_METADATA_KEY,
NEEDS_APPLY_DISCRIMINATED_UNION_METADATA_KEY,
CoreSchemaOrField,
define_expected_missing_refs,
Expand Down Expand Up @@ -535,10 +534,6 @@ def _model_schema(self, cls: type[BaseModel]) -> core_schema.CoreSchema:
new_inner_schema = define_expected_missing_refs(inner_schema, recursively_defined_type_refs())
if new_inner_schema is not None:
inner_schema = new_inner_schema
self._has_invalid_schema = True
metadata[HAS_INVALID_SCHEMAS_METADATA_KEY] = True
else:
metadata[HAS_INVALID_SCHEMAS_METADATA_KEY] = False
inner_schema = apply_model_validators(inner_schema, model_validators, 'inner')

model_schema = core_schema.model_schema(
Expand Down Expand Up @@ -674,11 +669,9 @@ def _post_process_generated_schema(self, schema: core_schema.CoreSchema) -> core
if 'metadata' in schema:
metadata = schema['metadata']
metadata[NEEDS_APPLY_DISCRIMINATED_UNION_METADATA_KEY] = self._needs_apply_discriminated_union
metadata[HAS_INVALID_SCHEMAS_METADATA_KEY] = self._has_invalid_schema
else:
schema['metadata'] = {
NEEDS_APPLY_DISCRIMINATED_UNION_METADATA_KEY: self._needs_apply_discriminated_union,
HAS_INVALID_SCHEMAS_METADATA_KEY: self._has_invalid_schema,
}
return schema

Expand Down
4 changes: 2 additions & 2 deletions pydantic/_internal/_model_construction.py
Expand Up @@ -488,12 +488,12 @@ def complete_model_class(
core_config = config_wrapper.core_config(cls)

schema = gen_schema.collect_definitions(schema)

schema = apply_discriminators(simplify_schema_references(schema))
if collect_invalid_schemas(schema):
set_model_mocks(cls, cls_name)
return False

schema = apply_discriminators(simplify_schema_references(schema))

# debug(schema)
cls.__pydantic_core_schema__ = schema = validate_core_schema(schema)
cls.__pydantic_validator__ = create_schema_validator(schema, core_config, config_wrapper.plugin_settings)
Expand Down
1 change: 0 additions & 1 deletion tests/test_model_signature.py
Expand Up @@ -32,7 +32,6 @@ class Model(BaseModel):
assert _equals(str(sig), '(*, a: float, b: int = 10) -> None')


@pytest.mark.xfail(reason='https://github.com/pydantic/pydantic/pull/7523#discussion_r1331765978')
def test_generic_model_signature():
T = TypeVar('T')

Expand Down

0 comments on commit 1377c04

Please sign in to comment.