-
Notifications
You must be signed in to change notification settings - Fork 892
fix(types): resolve Literal[None] instead of raising TypeError #1946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sarathfrancis90
wants to merge
1
commit into
dottxt-ai:main
Choose a base branch
from
sarathfrancis90:fix/literal-none-value
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -704,6 +704,23 @@ def test_dsl_literal_bool(): | |
| assert result_both == Alternatives([Regex("True"), Regex("False")]) | ||
|
|
||
|
|
||
| def test_dsl_literal_none(): | ||
| # Literal[None] previously raised "Type None is currently not supported" | ||
| # because the None value was recursed into as if it were a type. It should | ||
| # resolve to the bare ``None`` keyword, the same way Optional/Union render | ||
| # their None member. | ||
| result_none = python_types_to_terms(Literal[None]) | ||
| assert isinstance(result_none, Alternatives) | ||
| assert result_none.terms == [Regex("None")] | ||
|
|
||
| # None mixed with other literal values must resolve too. | ||
| result_mixed = python_types_to_terms(Literal["active", None]) | ||
| assert result_mixed == Alternatives([String("active"), Regex("None")]) | ||
|
|
||
| # A bare None value maps to the same bare keyword as it does in a union. | ||
| assert python_types_to_terms(None) == Regex("None") | ||
|
|
||
|
|
||
| def test_dsl_numeric_literal_escapes_regex_metacharacters(): | ||
| # A float's ``.`` must match literally, not act as a regex wildcard. | ||
| term = python_types_to_terms(1.5) | ||
|
|
@@ -1186,6 +1203,20 @@ def test_e2e_optional_none_not_quoted_in_containers(): | |
| assert not _re.fullmatch(literal_pattern, "[None]") | ||
|
|
||
|
|
||
| def test_e2e_list_literal_none(): | ||
| """``Literal[None]`` renders the bare ``None`` keyword, both standalone and | ||
| nested in a container, consistently with ``Optional``/``Union``.""" | ||
| standalone = to_regex(python_types_to_terms(Literal[None])) | ||
| assert _re.fullmatch(standalone, "None") | ||
| assert not _re.fullmatch(standalone, '"None"') | ||
|
|
||
| list_pattern = to_regex(python_types_to_terms(list[Literal["yes", None]])) | ||
| assert _re.fullmatch(list_pattern, "[None]") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| assert _re.fullmatch(list_pattern, '["yes", None]') | ||
| # The None branch stays a bare keyword and is not JSON-quoted like a string. | ||
| assert not _re.fullmatch(list_pattern, '["None"]') | ||
|
|
||
|
|
||
| def test_to_regex(): | ||
| string_term = String("hello") | ||
| assert to_regex(string_term) == r"hello" | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_handle_unionpeels off bothtype(None)andNonebecause typing normalizes the two, but this only catches the bare value:get_args(List[None])is(NoneType,), soList[None]/Dict[str, None]/Tuple[None, ...]still recurse into NoneType and hit the "Type NoneType is currently not supported" raise. Worth widening toif ptype is None or ptype is type(None)and adding a container case to the test?