Commit graph

34 commits

Author SHA1 Message Date
Ankush Menat
c9cdacb4ce
feat: Optionally force type checking on whitelisted methods (#36744) 2026-02-05 16:20:41 +05:30
Sagar Vora
e14218a671 refactor: simplify prepared_args creation, use prepared union type 2025-07-03 18:14:09 +05:30
Sagar Vora
9634fa58b0 refactor: define annotations earlier 2025-07-03 18:08:48 +05:30
Sagar Vora
f922af8001 refactor: avoid using callable as default 2025-07-03 17:30:30 +05:30
Sagar Vora
db246c0aa2 refactor: use global imports 2025-07-03 17:29:31 +05:30
Sagar Vora
fa7e9ebb75 chore: simplify TypeAdapter 2025-07-03 16:14:00 +05:30
Sagar Vora
6435a77e41 perf: cache signature params instead of signature 2025-03-20 00:55:52 +05:30
Ankush Menat
98cbe05c9a perf: use cached function signature 2025-01-06 11:23:30 +05:30
Ankush Menat
28aafcd708 perf: Don't validate types if there are no input types
Many functions just have return type annotated, in such cases running
validation code is not necessary.
2025-01-06 11:20:37 +05:30
David Arnold
e2a8c7fed3
test: fix universal type checker based on downstream use and more testing (#28619)
* test: fix universal type checker based on downstream use and more testing

* fix: type validation error reporting

* fix: types; various

* chore: switch off test-time type checking

still too many errors
2024-11-28 23:35:32 +01:00
David Arnold
9edd44de01
ci: this adds universal runtime typechecking during tests to test runners (#28554)
* ci: this adds universal runtime typechecking during tests to test runners

* ci: add configuration options for test-time type checking
2024-11-28 15:11:30 +00:00
Rohan
4bfcccfd11
fix: ignore unittest.mock objects during typing validations (#26301)
* fix: ignore unittest.mock objects during typing validations

* fix: check against base mock class

---------

Co-authored-by: Rohan Bansal <rohan@agritheory.dev>
2024-05-02 11:00:02 +00:00
Akhil Narang
3f1e19de85
refactor(treewide): enable RUF rules
Signed-off-by: Akhil Narang <me@akhilnarang.dev>
2024-02-21 16:20:28 +05:30
Akhil Narang
26ae0f3460
fix: ruff fixes
Signed-off-by: Akhil Narang <me@akhilnarang.dev>
2024-02-07 17:04:31 +05:30
Ankush Menat
de9ac89748 style: re-format with ruff 2024-02-05 18:53:33 +05:30
Ankush Menat
d5a21a2676 fix: rename type validation
`None` can be passed which is acceptable here.

TODO: Make slackdict accept none as bool and convert to False
2023-10-16 18:12:53 +05:30
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