crawlee-python icon indicating copy to clipboard operation
crawlee-python copied to clipboard

Add package for run-time type checking

Open vdusek opened this issue 2 years ago • 2 comments

Description

Based on the PR https://github.com/apify/apify-sdk-python/pull/171, @janbuchar suggested the usage of some run-time checking for Python.

E.g. typeguard, it can be applied either using a decorator @typechecked for a specific function or import hook typeguard.install_import_hook() for the whole module.

For some methods/functions where we check manually the type of args/return type it could make sense to use it. E.g. here https://github.com/apify/apify-sdk-python/blob/v1.5.1/src/apify/scrapy/utils.py#L44.

Potential problems

I suppose it is implemented by using typing.get_type_hints for getting the type hints for a specific function. I run into a bug when typing.get_type_hints and from __future__ import annotations are used together, see the issue https://github.com/apify/apify-sdk-python/issues/151. However, tests should reveal it.

vdusek avatar Jan 16 '24 15:01 vdusek

https://pypi.org/project/beartype/ is another option

janbuchar avatar Feb 28 '24 10:02 janbuchar

https://docs.pydantic.dev/latest/concepts/validation_decorator/ is also an option

janbuchar avatar Jul 24 '24 07:07 janbuchar

Closing this one as we utilized Pydantic for cases where it makes sense and there is probably no need for anything more.

vdusek avatar Nov 27 '24 08:11 vdusek