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.

Handling of typing.Literal

See original GitHub issue

Hey there! I’ve been putting pyrallis to action lately and can’t over emphasize how much cleaner it’s been to integrate and apply configurations.

That said, there are a couple use cases I’m interested to hear your thoughts on. First being how pyrallis handles typing.Literal. I see in decoding/decode_field that the Literal field will decode to ‘Any’ and that Literal[arg1, arg2, ..] will decode into arg1. In the case of Literal["constant"] this decodes to “constant”, which is not a type.

More succinctly, does the patternLiteral[object] make good sense to use and if so, what might be the way for pyralis to handle this type appropriately?

Traceback (most recent call last):
  File "/anaconda3/envs/spec/lib/python3.9/site-packages/pyrallis/parsers/decoding.py", line 65, in decode_dataclass
    field_value = decode_field(field, raw_value)
  File "/anaconda3/envs/spec/lib/python3.9/site-packages/pyrallis/parsers/decoding.py", line 102, in decode_field
    return decode(field_type, raw_value)
  File "/anaconda3/envs/spec/lib/python3.9/functools.py", line 877, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
  File "/anaconda3/envs/spec/lib/python3.9/site-packages/pyrallis/parsers/decoding.py", line 33, in decode
    return get_decoding_fn(t)(raw_value)
  File "/anaconda3/envs/spec/lib/python3.9/site-packages/pyrallis/parsers/decoding.py", line 176, in get_decoding_fn
    raise Exception(f"No decoding function for type {t}, consider using pyrallis.decode.register")
Exception: No decoding function for type typing.Literal['onecycle', 'constant'], consider using pyrallis.decode.register

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
phelps-matthewcommented, Feb 11, 2022

@eladrich Beautiful. Tested and this allowed me to get rid of all my encode/decode registry statements for Enums, thank you!!

1reaction
eladrichcommented, Feb 10, 2022

The latest release (v0.2.2) now uses name-based enums as suggested by @brentyi. @phelps-matthew Feel free to give it a try and let us know how it works for you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

typing — Support for type hints — Python 3.11.1 documentation
Source code: Lib/typing.py This module provides runtime support for type hints. The most fundamental support consists of the types Any, Union, Callable, ...
Read more >
Literal types and Enums - mypy 0.991 documentation
The basic rule is that literal types are treated as just regular subtypes of whatever type the parameter has. For example, Literal[3] is...
Read more >
Python Type Hints - How to Use typing.Literal - Adam Johnson
Literal for its type. This allows the type checker to make extra inferences, giving our code an increased level of safety.
Read more >
Handbook - Literal Types - TypeScript
A literal is a more concrete sub-type of a collective type. What this means is that "Hello World" is a string , but...
Read more >
Incomplete handling of Python 3.8's new typing.Literal
In PyCharm 2019.3, with Python 3.8, the new typing.Literal type hinting is displayed with an "Unresolved reference" warning if the literal contains more ......
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