Skip to content
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

Change codegen to treat typing.Union[Foo, NoneType] and typing.Optional[Foo] as the same #508

Merged
merged 1 commit into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions libcst/codegen/gen_matcher_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import re
from dataclasses import dataclass, fields
from typing import Generator, List, Optional, Sequence, Set, Tuple, Type, Union

Expand All @@ -12,6 +13,8 @@


CST_DIR: Set[str] = set(dir(cst))
CLASS_RE = r"<class \'(.*?)\'>"
OPTIONAL_RE = r"typing\.Union\[([^,]*?), NoneType]"


class CleanseFullTypeNames(cst.CSTTransformer):
Expand Down Expand Up @@ -396,8 +399,8 @@ def _get_clean_type_and_aliases(

# First, get the type as a parseable expression.
typestr = repr(typeobj)
if typestr.startswith("<class '") and typestr.endswith("'>"):
typestr = typestr[8:-2]
typestr = re.sub(CLASS_RE, r"\1", typestr)
typestr = re.sub(OPTIONAL_RE, r"typing.Optional[\1]", typestr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work for Union[A, B, NoneType] or Union[A, None]. While this might not cause an issue today, I don't want to be on the debugging end if it pops up in the future :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't it work for Union[A, B, NoneType], my regex rejects that.

I'm not sure I understand the difference between Union[A, NoneType] and Union[A, None]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, fair enough. apparently Option[Union[A, B]] always gets flattened to Union[A, B, NoneType] by dataclasses, so it doesn't even matter.


# Now, parse the expression with LibCST.
cleanser = CleanseFullTypeNames()
Expand Down Expand Up @@ -457,7 +460,7 @@ def _get_fields(node: Type[cst.CSTNode]) -> Generator[Field, None, None]:
generated_code.append("")
generated_code.append("# This file was generated by libcst.codegen.gen_matcher_classes")
generated_code.append("from dataclasses import dataclass")
generated_code.append("from typing import Callable, Sequence, Union")
generated_code.append("from typing import Callable, Optional, Sequence, Union")
generated_code.append("from typing_extensions import Literal")
generated_code.append("import libcst as cst")
generated_code.append("")
Expand Down
Loading