Skip to content

Commit

Permalink
Remove obsolete utilities, particularly strip_prefix
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 685674043
  • Loading branch information
oprypin authored and copybara-github committed Oct 14, 2024
1 parent 397715d commit 90eb3c5
Show file tree
Hide file tree
Showing 9 changed files with 6 additions and 38 deletions.
2 changes: 1 addition & 1 deletion pytype/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ def _create_module(self, ast):
)
members = {}
for val in data:
name = utils.strip_prefix(val.name, f"{ast.name}.")
name = val.name.removeprefix(f"{ast.name}.")
members[name] = val
return abstract.Module(self.ctx, ast.name, members, ast)

Expand Down
1 change: 0 additions & 1 deletion pytype/pyi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ py_library(
.classdef
.metadata
.types
pytype.utils
pytype.pytd.pytd_for_parser
pytype.pytd.parse.parse
)
Expand Down
3 changes: 1 addition & 2 deletions pytype/pyi/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import itertools
from typing import Any, TypeVar

from pytype import utils
from pytype.pyi import classdef
from pytype.pyi import metadata
from pytype.pyi import types
Expand Down Expand Up @@ -560,7 +559,7 @@ def matches_type(self, name: str, target: str | tuple[str, ...]):
name = f"{prefix}.{name_base}"
else:
name = self._resolve_alias(name)
name = utils.strip_prefix(name, parser_constants.EXTERNAL_NAME_PREFIX)
name = name.removeprefix(parser_constants.EXTERNAL_NAME_PREFIX)
if name == target:
return True
module, target_base = target.rsplit(".", 1)
Expand Down
2 changes: 0 additions & 2 deletions pytype/pytd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ py_library(
._pytd
.pytd_utils
.visitors
pytype.utils
pytype.pyi.parser
)

Expand Down Expand Up @@ -240,7 +239,6 @@ py_library(
.printer
.pytd_utils
.pytd_visitors
pytype.utils
pytype.pytd.parse.parse
)

Expand Down
4 changes: 2 additions & 2 deletions pytype/pytd/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def decrement_typing_count(self, member: str):

def get_alias(self, name: str):
if name.startswith("typing."):
return self._typing.members.get(utils.strip_prefix(name, "typing."))
return self._typing.members.get(name.removeprefix("typing."))
return self._reverse_alias_map.get(name)

def to_import_statements(self):
Expand Down Expand Up @@ -246,7 +246,7 @@ def _FromTyping(self, name):

def _StripUnitPrefix(self, name):
if self._unit:
return utils.strip_prefix(name, f"{self._unit.name}.")
return name.removeprefix(f"{self._unit.name}.")
else:
return name

Expand Down
3 changes: 1 addition & 2 deletions pytype/pytd/serialize_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


import msgspec
from pytype import utils
from pytype.pyi import parser
from pytype.pytd import pytd
from pytype.pytd import pytd_utils
Expand Down Expand Up @@ -43,7 +42,7 @@ def __init__(self):
def EnterTypeDeclUnit(self, node):
for alias in node.aliases:
if isinstance(alias.type, pytd.Module):
name = utils.strip_prefix(alias.name, f"{node.name}.")
name = alias.name.removeprefix(f"{node.name}.")
self._module_aliases[name] = alias.type.module_name

def VisitLateType(self, node):
Expand Down
5 changes: 1 addition & 4 deletions pytype/pytd/visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from pytype import datatypes
from pytype import module_utils
from pytype import utils
from pytype.pytd import base_visitor
from pytype.pytd import escape
from pytype.pytd import mro
Expand Down Expand Up @@ -1321,9 +1320,7 @@ class StripExternalNamePrefix(Visitor):
"""

def VisitNamedType(self, node):
new_name = utils.strip_prefix(
node.name, parser_constants.EXTERNAL_NAME_PREFIX
)
new_name = node.name.removeprefix(parser_constants.EXTERNAL_NAME_PREFIX)
return node.Replace(name=new_name)


Expand Down
17 changes: 0 additions & 17 deletions pytype/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import collections
import contextlib
import itertools
import keyword
import re
import threading
import traceback
import weakref
Expand Down Expand Up @@ -79,13 +77,6 @@ def validate_version(python_version):
raise UsageError("Python versions > 3.12 are not yet supported.")


def strip_prefix(string, prefix):
"""Strip off prefix if it exists."""
if string.startswith(prefix):
return string[len(prefix) :]
return string


def maybe_truncate(s, length=30):
"""Truncate long strings (and append '...'), but leave short strings alone."""
s = str(s)
Expand Down Expand Up @@ -131,14 +122,6 @@ def pretty_dnf(dnf):
return " | ".join(pretty_conjunction(c) for c in dnf)


def numeric_sort_key(s):
return tuple((int(e) if e.isdigit() else e) for e in re.split(r"(\d+)", s))


def concat_tuples(tuples):
return tuple(itertools.chain.from_iterable(tuples))


def native_str(s: str | bytes, errors: str = "strict") -> str:
"""Convert a bytes object to the native str type."""
if isinstance(s, str):
Expand Down
7 changes: 0 additions & 7 deletions pytype/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
class UtilsTest(unittest.TestCase):
"""Test generic utilities."""

def test_numeric_sort_key(self):
k = utils.numeric_sort_key
self.assertLess(k("1aaa"), k("12aa"))
self.assertLess(k("12aa"), k("123a"))
self.assertLess(k("a1aa"), k("a12a"))
self.assertLess(k("a12a"), k("a123"))

def test_pretty_dnf(self):
dnf = [["a", "b"], "c", ["d", "e", "f"]]
self.assertEqual(utils.pretty_dnf(dnf), "(a & b) | c | (d & e & f)")
Expand Down

0 comments on commit 90eb3c5

Please sign in to comment.