Commit graph

18 commits

Author SHA1 Message Date
Corentin Flr
1380241609
chore: Switch to pydantic.ConfigDict in typing_validations.py (#22268)
> PydanticDeprecatedSince20: Support for class-based `config` is deprecated, use ConfigDict instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.0/migration/
2023-09-02 11:23:50 +05:30
Ankush Menat
adf30693a9 ci: update pyupgrade 2023-07-14 14:24:08 +05:30
Gavin D'souza
883445aefa
perf: Defer pydantic imports until function call
Pydantic adds an additional 1-2MB in memory usage. We can defer it in
case an environment doesn't use it at all.
2023-07-02 16:00:50 +05:30
Gavin D'souza
ee3d4da734
fix: Allow arbitrary objects validation in TypeAdapter 2023-06-07 19:26:57 +05:30
Gavin D'souza
1a7b7a589b
build(deps): Bump Pydantic from v1 to v2 2023-06-07 17:03:59 +05:30
Gavin D'souza
d66eed129c refactor: validate_argument_types
* Rename API for ease of public use
* Add validation condition parameter
* Move function to utils module instead of frappe namespace
2022-12-19 15:07:03 +05:30
Gavin D'souza
d978ed7d06 refactor: Raise FrappeTypeError in case of type mismatches 2022-12-15 13:47:05 +05:30
Gavin D'souza
ec931004ca refactor: transform_parameter_types
Failfast if args and kwargs or annotations don't exist
2022-12-15 13:47:05 +05:30
Gavin D'souza
579aa124e1 fix(type-check): Convert tuple types to Union 2022-12-15 13:47:05 +05:30
Gavin D'souza
299a017081 fix(type-check): Parse and validate arbitrary objects 2022-12-15 13:47:05 +05:30
Gavin D'souza
4fe260e09e refactor: transform_parameter_types
- Switch to Pydantic which is under continuous development and can
  support more types
- Equivalent Pydantic API will try to transform data if possible
- The previous point makes it such that we don't need to explicitly try
  to parse each stringified int in app code since Pydantic can do this
- Drop typeguard since it did not handle 3.10+ native typing definitions
2022-12-15 13:47:05 +05:30
Gavin D'souza
73b0971a26 test: Add tests for typing validations 2022-12-15 13:47:05 +05:30
Gavin D'souza
41d6c791f1 fix: Build args dict correctly 2022-12-15 13:47:05 +05:30
Gavin D'souza
8e125f577e fix(type-check): Allow some slack for bool to be int or float 2022-12-15 13:47:05 +05:30
Gavin D'souza
f3250808f0 fix: Use typeguard to handle base hints
Pre process exceptions that Frappe requires and pass the ruleset to
typeguard's check_type API
2022-12-15 13:47:05 +05:30
Gavin D'souza
d8cd1be23b fix: Skip type checking if any allowed type is stringified 2022-12-15 13:47:05 +05:30
Gavin D'souza
6678007a8d fix: Support missing Optional or mismatching annotation types
Allow default types as acceptable values if not included in typing
hints. A common example of this is:

def test_password_strength(new_password: str, user_data: Sequence = None):

where Optional[Sequence] is implicit since default value is None.
2022-12-15 13:47:05 +05:30
Gavin D'souza
3fd74afa47 feat(whitelisted): Runtime typing hints validation
- Run type validations if annotations exist for whitelisted functions
- Run validations only on function calls in presense of frappe.local.request

In action:

```bash
> curl -H 'Content-Type: application/json' 'http://photos:8000/api/method/frappe.handler.download_file' -d '{"file_url": ["!=", "gavin.jpg"]}'
```

Note: This ignores stringified or ForwardRef types. If you want types to
be validated make sure they are not imported under `if TYPE_CHECKING`
blocks
2022-12-15 13:47:05 +05:30