question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve performance of get type hints

See original GitHub issue

Calling get_type_hints consume a lot of CPU time. When calling the from_dict multiple times with the same Class type this optimization can save around 50% of the runtime.

Suggestion for the change:

` @lru_cache(maxsize=100) def _get_type_hint_with_cache(_data_class): return get_type_hints(_data_class, None)

def from_dict(data_class: Type[T], data: Data, config: Optional[Config] = None) -> T:

init_values: Data = {}
post_init_values: Data = {}
config = config or Config()
try:
    if config.forward_references:
        data_class_hints = get_type_hints(data_class, globalns=config.forward_references)
    else:
        data_class_hints = _get_type_hint_with_cache(data_class)

except NameError as error:
    raise ForwardReferenceError(str(error))
data_class_fields = get_fields(data_class)
if config.strict:
    extra_fields = set(data.keys()) - {f.name for f in data_class_fields}
    if extra_fields:
        raise UnexpectedDataError(keys=extra_fields)
for field in data_class_fields:
    field = copy.copy(field)
    field.type = data_class_hints[field.name]
    try:
        try:
            field_data = data[field.name]
            transformed_value = transform_value(
                type_hooks=config.type_hooks, cast=config.cast, target_type=field.type, value=field_data
            )
            value = _build_value(type_=field.type, data=transformed_value, config=config)
        except DaciteFieldError as error:
            error.update_path(field.name)
            raise
        if config.check_types and not is_instance(value, field.type):
            raise WrongTypeError(field_path=field.name, field_type=field.type, value=value)
    except KeyError:
        try:
            value = get_default_value_for_field(field)
        except DefaultValueNotFoundError:
            if not field.init:
                continue
            raise MissingValueError(field.name)
    if field.init:
        init_values[field.name] = value
    else:
        post_init_values[field.name] = value

return create_instance(data_class=data_class, init_values=init_values, post_init_values=post_init_values)

`

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
marcoaciernocommented, Mar 19, 2021

Are there any news regarding this? 😃

1reaction
uri-pcommented, Jun 29, 2020

Very useful !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Does Type Hints improve performance in Python? [closed]
A natural future evolution might see such type hints used for byte code optimization. No, if you use the python interpreter to execute...
Read more >
12 Beginner Concepts About Type Hints To Improve Your ...
12 Beginner Concepts About Type Hints To Improve Your Python Code · 2 — Perform static type checking with mypy · 5 —Dict...
Read more >
Python Type Hinting and Speed - Medium
Type Hints are annotations that specify the runtime type of value within your Python codes. This look statically typed, right?
Read more >
Pros and Cons of Type Hints - Real Python
Type hints improve IDEs and linters. They make it much easier to statically reason about your code. Type hints help you build and...
Read more >
You Should Start Using Python Type Hints Right Now
There's no effect on performance whatsoever with or without type hints. Cpython ignore type hints similar to how comments are discarded during ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found