Commit graph

13 commits

Author SHA1 Message Date
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