Skip to content

Commit

Permalink
Merge pull request #293 from Carreau/clean-six
Browse files Browse the repository at this point in the history
Reify six.string_types to str.
  • Loading branch information
Carreau authored Jan 30, 2024
2 parents 343227d + ae92f7b commit 6b1e28c
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 37 deletions.
4 changes: 2 additions & 2 deletions lib/python/pyflyby/_autoimp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1489,7 +1489,7 @@ def find_missing_imports(arg, namespaces):
``list`` of ``DottedIdentifier``
"""
namespaces = ScopeStack(namespaces)
if isinstance(arg, (DottedIdentifier, six.string_types)):
if isinstance(arg, (DottedIdentifier, str)):
try:
arg = DottedIdentifier(arg)
except BadDottedIdentifierError:
Expand Down Expand Up @@ -1897,7 +1897,7 @@ def auto_eval(arg, filename=None, mode=None,
"""
if isinstance(flags, int):
assert isinstance(flags, CompilerFlags)
if isinstance(arg, (six.string_types, Filename, FileText, PythonBlock)):
if isinstance(arg, (str, Filename, FileText, PythonBlock)):
block = PythonBlock(arg, filename=filename, flags=flags,
auto_flags=auto_flags)
flags = block.flags
Expand Down
17 changes: 8 additions & 9 deletions lib/python/pyflyby/_dbg.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# License: MIT http://opensource.org/licenses/MIT



import builtins
from contextlib import contextmanager
import errno
from functools import wraps
Expand All @@ -14,9 +14,6 @@
import time
from types import CodeType, FrameType, TracebackType

import six
from six.moves import builtins

from collections.abc import Callable

from pyflyby._file import Filename
Expand Down Expand Up @@ -349,7 +346,8 @@ def _debug_code(arg, globals=None, locals=None, auto_import=True, tty="/dev/tty"
print("Entering debugger. Use 'n' to step, 'c' to run, 'q' to stop.")
print("")
from ._parse import PythonStatement, PythonBlock, FileText
if isinstance(arg, (six.string_types, PythonStatement, PythonBlock, FileText)):

if isinstance(arg, (str, PythonStatement, PythonBlock, FileText)):
# Compile the block so that we can get the right compile mode.
arg = PythonBlock(arg)
# TODO: enter text into linecache
Expand Down Expand Up @@ -502,8 +500,9 @@ def debugger(*args, **kwargs):
# TODO: capture globals/locals when relevant.
wait_for_debugger_to_attach(arg)
return
if isinstance(arg, (six.string_types, PythonStatement, PythonBlock, FileText,
CodeType, Callable)):
if isinstance(
arg, (str, PythonStatement, PythonBlock, FileText, CodeType, Callable)
):
_debug_code(arg, globals=globals, locals=locals, tty=tty)
on_continue()
return
Expand Down Expand Up @@ -1013,12 +1012,12 @@ def inject(pid, statements, wait=True, show_gdb_output=False):
"""
import subprocess
os.kill(pid, 0) # raises OSError "No such process" unless pid exists
if isinstance(statements, six.string_types):
if isinstance(statements, str):
statements = (statements,)
else:
statements = tuple(statements)
for statement in statements:
if not isinstance(statement, six.string_types):
if not isinstance(statement, str):
raise TypeError(
"Expected iterable of strings, not %r" % (type(statement),))
# Based on
Expand Down
11 changes: 4 additions & 7 deletions lib/python/pyflyby/_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@
import io
import os
import re
import six
import sys

from six import string_types

from pyflyby._util import cached_attribute, cmp, memoize


Expand All @@ -34,13 +31,13 @@ class Filename(object):
def __new__(cls, arg):
if isinstance(arg, cls):
return arg
if isinstance(arg, six.string_types):
if isinstance(arg, str):
return cls._from_filename(arg)
raise TypeError

@classmethod
def _from_filename(cls, filename):
if not isinstance(filename, six.string_types):
if not isinstance(filename, str):
raise TypeError
filename = str(filename)
if not filename:
Expand Down Expand Up @@ -372,7 +369,7 @@ def __new__(cls, arg, filename=None, startpos=None):
return cls(read_file(arg), filename=filename, startpos=startpos)
elif hasattr(arg, "__text__"):
return FileText(arg.__text__(), filename=filename, startpos=startpos)
elif isinstance(arg, six.string_types):
elif isinstance(arg, str):
self = object.__new__(cls)
self.joined = arg
else:
Expand All @@ -389,7 +386,7 @@ def __new__(cls, arg, filename=None, startpos=None):
def _from_lines(cls, lines, filename, startpos):
assert type(lines) is tuple
assert len(lines) > 0
assert isinstance(lines[0], string_types)
assert isinstance(lines[0], str)
assert not lines[-1].endswith("\n")
self = object.__new__(cls)
self.lines = lines
Expand Down
5 changes: 2 additions & 3 deletions lib/python/pyflyby/_idents.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from functools import total_ordering
from keyword import kwlist
import re
import six

from pyflyby._util import cached_attribute, cmp

Expand Down Expand Up @@ -111,7 +110,7 @@ def is_identifier(s, dotted=False, prefix=False):
:rtype:
``bool``
"""
if not isinstance(s, six.string_types):
if not isinstance(s, str):
raise TypeError("is_identifier(): expected a string; got a %s"
% (type(s).__name__,))
if prefix:
Expand Down Expand Up @@ -144,7 +143,7 @@ class DottedIdentifier(object):
def __new__(cls, arg, scope_info=None):
if isinstance(arg, cls):
return arg
if isinstance(arg, six.string_types):
if isinstance(arg, str):
return cls._from_name(arg, scope_info)
if isinstance(arg, (tuple, list)):
return cls._from_name(".".join(arg), scope_info)
Expand Down
10 changes: 5 additions & 5 deletions lib/python/pyflyby/_importdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,27 +504,27 @@ def _from_filenames(cls, filenames, _mandatory_filenames_deprecated=[]):

@classmethod
def _parse_import_set(cls, arg):
if isinstance(arg, six.string_types):
if isinstance(arg, str):
arg = [arg]
if not isinstance(arg, (tuple, list)):
raise ValueError("Expected a list, not a %s" % (type(arg).__name__,))
for item in arg:
if not isinstance(item, six.string_types):
if not isinstance(item, str):
raise ValueError(
"Expected a list of str, not %s" % (type(item).__name__,))
return ImportSet(arg)

@classmethod
def _parse_import_map(cls, arg):
if isinstance(arg, six.string_types):
if isinstance(arg, str):
arg = [arg]
if not isinstance(arg, dict):
raise ValueError("Expected a dict, not a %s" % (type(arg).__name__,))
for k, v in arg.items():
if not isinstance(k, six.string_types):
if not isinstance(k, str):
raise ValueError(
"Expected a dict of str, not %s" % (type(k).__name__,))
if not isinstance(v, six.string_types):
if not isinstance(v, str):
raise ValueError(
"Expected a dict of str, not %s" % (type(v).__name__,))
return ImportMap(arg)
Expand Down
2 changes: 1 addition & 1 deletion lib/python/pyflyby/_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ def _list_members_for_completion(obj, ip):
pass
else:
words = dir(obj)
return [w for w in words if isinstance(w, six.string_types)]
return [w for w in words if isinstance(w, str)]


def _auto_import_in_pdb_frame(pdb_instance, arg):
Expand Down
3 changes: 2 additions & 1 deletion lib/python/pyflyby/_livepatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,8 @@ def _format_age(t):
def _interpret_module(arg):
def mod_fn(module):
return getattr(module, "__file__", None)
if isinstance(arg, six.string_types):

if isinstance(arg, str):
try:
return sys.modules[arg]
except KeyError:
Expand Down
7 changes: 3 additions & 4 deletions tests/test_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import readline
import requests
from shutil import rmtree
import six
from six import BytesIO
from subprocess import check_call
import sys
Expand Down Expand Up @@ -514,7 +513,7 @@ def _build_pythonpath(PYTHONPATH):
pypath = [os.path.dirname(os.path.dirname(pyflyby.__file__))]
if sys.version_info < (3, 9):
pypath += _extra_readline_pythonpath_dirs()
if isinstance(PYTHONPATH, (Filename, six.string_types)):
if isinstance(PYTHONPATH, (Filename, str)):
PYTHONPATH = [PYTHONPATH]
PYTHONPATH = [str(Filename(d)) for d in PYTHONPATH]
pypath += PYTHONPATH
Expand Down Expand Up @@ -583,7 +582,7 @@ def _build_ipython_cmd(ipython_dir, prog="ipython", args=[], autocall=False, fro
else:
# Get the program from the python that is running.
cmd += [os.path.join(os.path.dirname(sys.executable), prog)]
if isinstance(args, six.string_types):
if isinstance(args, str):
args = [args]
if args and not args[0].startswith("-"):
app = args[0]
Expand Down Expand Up @@ -1043,7 +1042,7 @@ def ipython(template, **kwargs):
template = dedent(template).strip()
input, expected = parse_template(template, clear_tab_completions=_IPYTHON_VERSION>=(7,))
args = kwargs.pop("args", ())
if isinstance(args, six.string_types):
if isinstance(args, str):
args = [args]
args = list(args)
if args and not args[0].startswith("-"):
Expand Down
7 changes: 2 additions & 5 deletions tests/test_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
import os
import pytest
from shutil import rmtree
import six
import subprocess
import sys
import tempfile
from tempfile import NamedTemporaryFile, mkdtemp
from textwrap import dedent

from six import string_types

import pyflyby
from pyflyby._file import Filename
from pyflyby._util import cached_attribute
Expand All @@ -30,7 +27,7 @@
def flatten(args):
result = []
for arg in args:
if isinstance(arg, six.string_types):
if isinstance(arg, str):
result.append(arg)
else:
try:
Expand Down Expand Up @@ -100,7 +97,7 @@ def _build_pythonpath(PYTHONPATH):
``str``
"""
pypath = [os.path.dirname(os.path.dirname(pyflyby.__file__))]
if isinstance(PYTHONPATH, (Filename,) + string_types):
if isinstance(PYTHONPATH, (Filename, str)):
PYTHONPATH = [PYTHONPATH]
PYTHONPATH = [str(Filename(d)) for d in PYTHONPATH]
pypath += PYTHONPATH
Expand Down

0 comments on commit 6b1e28c

Please sign in to comment.