Skip to content

Commit 2e6d95b

Browse files
authored
we can use recent typing for stub file or pure type hint (#202)
1 parent 294aed0 commit 2e6d95b

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

pydantic_core/_pydantic_core.pyi

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
from typing import Any, Dict, List, Optional, TypedDict, Union
2+
from typing import Any, TypedDict
33

44
from pydantic_core._types import Config, Schema
55

@@ -12,39 +12,37 @@ __all__ = '__version__', 'SchemaValidator', 'SchemaError', 'ValidationError', 'P
1212
__version__: str
1313

1414
class SchemaValidator:
15-
def __init__(self, schema: Schema, config: Optional[Config] = None) -> None: ...
16-
def validate_python(self, input: Any, strict: Optional[bool] = None, context: Any = None) -> Any: ...
17-
def isinstance_python(self, input: Any, strict: Optional[bool] = None, context: Any = None) -> bool: ...
15+
def __init__(self, schema: Schema, config: 'Config | None' = None) -> None: ...
16+
def validate_python(self, input: Any, strict: 'bool | None' = None, context: Any = None) -> Any: ...
17+
def isinstance_python(self, input: Any, strict: 'bool | None' = None, context: Any = None) -> bool: ...
1818
def validate_json(
19-
self, input: Union[str, bytes, bytearray], strict: Optional[bool] = None, context: Any = None
19+
self, input: 'str | bytes | bytearray', strict: 'bool | None' = None, context: Any = None
2020
) -> Any: ...
2121
def isinstance_json(
22-
self, input: Union[str, bytes, bytearray], strict: Optional[bool] = None, context: Any = None
22+
self, input: 'str | bytes | bytearray', strict: 'bool | None' = None, context: Any = None
2323
) -> bool: ...
24-
def validate_assignment(self, field: str, input: Any, data: Dict[str, Any]) -> Dict[str, Any]: ...
24+
def validate_assignment(self, field: str, input: Any, data: 'dict[str, Any]') -> 'dict[str, Any]': ...
2525

2626
class SchemaError(Exception):
2727
pass
2828

2929
class ErrorDetails(TypedDict):
3030
kind: str
31-
loc: List[Union[int, str]]
31+
loc: 'list[int | str]'
3232
message: str
3333
input_value: Any
34-
context: NotRequired[Dict[str, Any]]
34+
context: NotRequired['dict[str, Any]']
3535

3636
class ValidationError(ValueError):
3737
title: str
3838

3939
def error_count(self) -> int: ...
40-
def errors(self) -> List[ErrorDetails]: ...
40+
def errors(self) -> 'list[ErrorDetails]': ...
4141

4242
class PydanticValueError(ValueError):
4343
kind: str
4444
message_template: str
45-
context: Optional[Dict[str, Union[str, int]]]
45+
context: 'dict[str, str | int] | None'
4646

47-
def __init__(
48-
self, kind: str, message_template: str, context: Optional[Dict[str, Union[str, int]]] = None
49-
) -> None: ...
47+
def __init__(self, kind: str, message_template: str, context: 'dict[str, str | int] | None' = None) -> None: ...
5048
def message(self) -> str: ...

tests/conftest.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import re
66
from dataclasses import dataclass
77
from pathlib import Path
8-
from typing import Any, Optional, Type
8+
from typing import Any, Type
99

1010
import pytest
1111
from hypothesis import settings
@@ -41,20 +41,20 @@ def __repr__(self):
4141

4242

4343
class PyAndJsonValidator:
44-
def __init__(self, schema, validator_type: Optional[Literal['json', 'python']] = None):
44+
def __init__(self, schema, validator_type: 'Literal["json", "python"] | None' = None):
4545
self.validator = SchemaValidator(schema)
4646
self.validator_type = validator_type
4747

48-
def validate_python(self, py_input, strict: Optional[bool] = None):
48+
def validate_python(self, py_input, strict: 'bool | None' = None):
4949
return self.validator.validate_python(py_input, strict)
5050

51-
def validate_test(self, py_input, strict: Optional[bool] = None):
51+
def validate_test(self, py_input, strict: 'bool | None' = None):
5252
if self.validator_type == 'json':
5353
return self.validator.validate_json(json.dumps(py_input), strict)
5454
elif self.validator_type == 'python':
5555
return self.validator.validate_python(py_input, strict)
5656

57-
def isinstance_test(self, py_input, strict: Optional[bool] = None):
57+
def isinstance_test(self, py_input, strict: 'bool | None' = None):
5858
if self.validator_type == 'json':
5959
return self.validator.isinstance_json(json.dumps(py_input), strict)
6060
elif self.validator_type == 'python':
@@ -87,7 +87,7 @@ def tmp_work_path(tmp_path: Path):
8787

8888
@pytest.fixture
8989
def import_execute(request, tmp_work_path: Path):
90-
def _import_execute(source: str, *, custom_module_name: Optional[str] = None):
90+
def _import_execute(source: str, *, custom_module_name: 'str | None' = None):
9191
example_bash_file = tmp_work_path / 'example.sh'
9292
example_bash_file.write_text('#!/bin/sh\necho testing')
9393
example_bash_file.chmod(0o755)

0 commit comments

Comments
 (0)