forked from google/sedpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes google#49 Used python script to automate changes: ```python import re from pathlib import Path project_dir = Path("path") optional_pattern = re.compile(r"Optional\[(.+)\]") def replace_optional(match): '''Replace "Optional[...]" with "... | None"''' content = match.group(1) bracket_count = 0 end_index = -1 for i, char in enumerate(content): if char == '[': bracket_count += 1 elif char == ']': bracket_count -= 1 if bracket_count == 0: end_index = i break if end_index != -1: return f"{content[:end_index + 1]} | None{content[end_index + 1:]}" else: return f"{content} | None" patterns = [ (re.compile(r"\bList\b"), "list"), (re.compile(r"\bDict\b"), "dict"), (re.compile(r"\bTuple\b"), "tuple"), ] for filepath in project_dir.rglob("*.py"): with open(filepath, "r", encoding="utf-8") as file: content = file.read() content = optional_pattern.sub(replace_optional, content) for pattern, replacement in patterns: content = pattern.sub(replacement, content) with open(filepath, "w", encoding="utf-8") as file: file.write(content) ``` --------- Co-authored-by: kralka <[email protected]>
- Loading branch information
1 parent
2555cf6
commit 65fc059
Showing
8 changed files
with
79 additions
and
86 deletions.
There are no files selected for viewing
This file contains 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 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 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
Oops, something went wrong.