[clang] [libclang/python] Clean up tests (PR #114383)

Jannick Kremer via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 31 02:39:47 PDT 2024


https://github.com/DeinAlptraum created https://github.com/llvm/llvm-project/pull/114383

Clean up imports via isort
Remove unused imports
Remove unused variables
Remove Python <3.6 compatibility measures

>From 25e10c362338265e07ec045b2b8bbe692ae18092 Mon Sep 17 00:00:00 2001
From: Jannick Kremer <jannick.kremer at mailbox.org>
Date: Sat, 24 Aug 2024 11:33:22 +0200
Subject: [PATCH] [libclang/python] Clean up tests

Clean up imports via isort
Remove duplicate imports
Remove Python <3.6 compatibility measures
---
 .../tests/cindex/test_access_specifiers.py    | 12 ++--
 .../bindings/python/tests/cindex/test_cdb.py  | 29 ++++------
 .../tests/cindex/test_code_completion.py      | 15 ++---
 .../python/tests/cindex/test_comment.py       |  8 +--
 .../python/tests/cindex/test_cursor.py        | 31 +++++-----
 .../python/tests/cindex/test_cursor_kind.py   |  5 +-
 .../python/tests/cindex/test_diagnostics.py   |  7 +--
 .../python/tests/cindex/test_enums.py         | 16 ++---
 .../test_exception_specification_kind.py      | 11 ++--
 .../bindings/python/tests/cindex/test_file.py |  5 +-
 .../python/tests/cindex/test_index.py         |  8 +--
 .../python/tests/cindex/test_linkage.py       | 12 ++--
 .../python/tests/cindex/test_location.py      | 19 +++---
 .../python/tests/cindex/test_rewrite.py       | 10 +---
 .../python/tests/cindex/test_source_range.py  |  4 +-
 .../python/tests/cindex/test_tls_kind.py      | 12 ++--
 .../python/tests/cindex/test_token_kind.py    |  5 +-
 .../python/tests/cindex/test_tokens.py        | 11 +---
 .../tests/cindex/test_translation_unit.py     | 58 ++++++++-----------
 .../bindings/python/tests/cindex/test_type.py | 18 ++----
 clang/bindings/python/tests/cindex/util.py    | 20 +------
 21 files changed, 121 insertions(+), 195 deletions(-)

diff --git a/clang/bindings/python/tests/cindex/test_access_specifiers.py b/clang/bindings/python/tests/cindex/test_access_specifiers.py
index c1cc18ebe6e589..ca2bbd3cc86117 100644
--- a/clang/bindings/python/tests/cindex/test_access_specifiers.py
+++ b/clang/bindings/python/tests/cindex/test_access_specifiers.py
@@ -1,18 +1,14 @@
 import os
-from clang.cindex import Config
+
+from clang.cindex import AccessSpecifier, Config
 
 if "CLANG_LIBRARY_PATH" in os.environ:
     Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
 
-from clang.cindex import AccessSpecifier
-from clang.cindex import Cursor
-from clang.cindex import TranslationUnit
-
-from .util import get_cursor
-from .util import get_tu
-
 import unittest
 
+from .util import get_cursor, get_tu
+
 
 class TestAccessSpecifiers(unittest.TestCase):
     def test_access_specifiers(self):
diff --git a/clang/bindings/python/tests/cindex/test_cdb.py b/clang/bindings/python/tests/cindex/test_cdb.py
index a5cc22796aa2ad..d0adbb36d66f2a 100644
--- a/clang/bindings/python/tests/cindex/test_cdb.py
+++ b/clang/bindings/python/tests/cindex/test_cdb.py
@@ -1,20 +1,14 @@
 import os
-from clang.cindex import Config
+
+from clang.cindex import CompilationDatabase, CompilationDatabaseError, Config
 
 if "CLANG_LIBRARY_PATH" in os.environ:
     Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
 
-from clang.cindex import CompilationDatabase
-from clang.cindex import CompilationDatabaseError
-from clang.cindex import CompileCommands
-from clang.cindex import CompileCommand
-import os
 import gc
-import unittest
 import sys
-from .util import skip_if_no_fspath
-from .util import str_to_path
-
+import unittest
+from pathlib import Path
 
 kInputsDir = os.path.join(os.path.dirname(__file__), "INPUTS")
 
@@ -31,7 +25,7 @@ def test_create_fail(self):
         with open(os.devnull, "wb") as null:
             os.dup2(null.fileno(), 2)
         with self.assertRaises(CompilationDatabaseError) as cm:
-            cdb = CompilationDatabase.fromDirectory(path)
+            CompilationDatabase.fromDirectory(path)
         os.dup2(stderr, 2)
         os.close(stderr)
 
@@ -40,7 +34,7 @@ def test_create_fail(self):
 
     def test_create(self):
         """Check we can load a compilation database"""
-        cdb = CompilationDatabase.fromDirectory(kInputsDir)
+        CompilationDatabase.fromDirectory(kInputsDir)
 
     def test_lookup_succeed(self):
         """Check we get some results if the file exists in the db"""
@@ -48,13 +42,10 @@ def test_lookup_succeed(self):
         cmds = cdb.getCompileCommands("/home/john.doe/MyProject/project.cpp")
         self.assertNotEqual(len(cmds), 0)
 
-    @skip_if_no_fspath
     def test_lookup_succeed_pathlike(self):
         """Same as test_lookup_succeed, but with PathLikes"""
-        cdb = CompilationDatabase.fromDirectory(str_to_path(kInputsDir))
-        cmds = cdb.getCompileCommands(
-            str_to_path("/home/john.doe/MyProject/project.cpp")
-        )
+        cdb = CompilationDatabase.fromDirectory(Path(kInputsDir))
+        cmds = cdb.getCompileCommands(Path("/home/john.doe/MyProject/project.cpp"))
         self.assertNotEqual(len(cmds), 0)
 
     def test_all_compilecommand(self):
@@ -175,7 +166,7 @@ def test_compilationDB_references(self):
         cmds = cdb.getCompileCommands("/home/john.doe/MyProject/project.cpp")
         del cdb
         gc.collect()
-        workingdir = cmds[0].directory
+        cmds[0].directory
 
     def test_compilationCommands_references(self):
         """Ensure CompilationsCommand keeps a reference to CompilationCommands"""
@@ -185,4 +176,4 @@ def test_compilationCommands_references(self):
         cmd0 = cmds[0]
         del cmds
         gc.collect()
-        workingdir = cmd0.directory
+        cmd0.directory
diff --git a/clang/bindings/python/tests/cindex/test_code_completion.py b/clang/bindings/python/tests/cindex/test_code_completion.py
index 1d513dbca25364..c7a86aa82a8ebc 100644
--- a/clang/bindings/python/tests/cindex/test_code_completion.py
+++ b/clang/bindings/python/tests/cindex/test_code_completion.py
@@ -1,14 +1,12 @@
 import os
-from clang.cindex import Config
+
+from clang.cindex import Config, TranslationUnit
 
 if "CLANG_LIBRARY_PATH" in os.environ:
     Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
 
-from clang.cindex import TranslationUnit
-
 import unittest
-from .util import skip_if_no_fspath
-from .util import str_to_path
+from pathlib import Path
 
 
 class TestCodeCompletion(unittest.TestCase):
@@ -57,11 +55,10 @@ def test_code_complete(self):
         ]
         self.check_completion_results(cr, expected)
 
-    @skip_if_no_fspath
     def test_code_complete_pathlike(self):
         files = [
             (
-                str_to_path("fake.c"),
+                Path("fake.c"),
                 """
 /// Aaa.
 int test1;
@@ -77,14 +74,14 @@ def test_code_complete_pathlike(self):
         ]
 
         tu = TranslationUnit.from_source(
-            str_to_path("fake.c"),
+            Path("fake.c"),
             ["-std=c99"],
             unsaved_files=files,
             options=TranslationUnit.PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION,
         )
 
         cr = tu.codeComplete(
-            str_to_path("fake.c"),
+            Path("fake.c"),
             9,
             1,
             unsaved_files=files,
diff --git a/clang/bindings/python/tests/cindex/test_comment.py b/clang/bindings/python/tests/cindex/test_comment.py
index 265c6d3d73de04..1ecbb42c18ffca 100644
--- a/clang/bindings/python/tests/cindex/test_comment.py
+++ b/clang/bindings/python/tests/cindex/test_comment.py
@@ -1,14 +1,14 @@
 import os
-from clang.cindex import Config
+
+from clang.cindex import Config, TranslationUnit
 
 if "CLANG_LIBRARY_PATH" in os.environ:
     Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
 
-from clang.cindex import TranslationUnit
-from tests.cindex.util import get_cursor
-
 import unittest
 
+from .util import get_cursor
+
 
 class TestComment(unittest.TestCase):
     def test_comment(self):
diff --git a/clang/bindings/python/tests/cindex/test_cursor.py b/clang/bindings/python/tests/cindex/test_cursor.py
index 77d8ca415708f8..a36c6e803887fb 100644
--- a/clang/bindings/python/tests/cindex/test_cursor.py
+++ b/clang/bindings/python/tests/cindex/test_cursor.py
@@ -1,24 +1,23 @@
 import os
-from clang.cindex import Config
+
+from clang.cindex import (
+    AvailabilityKind,
+    BinaryOperator,
+    Config,
+    CursorKind,
+    StorageClass,
+    TemplateArgumentKind,
+    TranslationUnit,
+    TypeKind,
+)
 
 if "CLANG_LIBRARY_PATH" in os.environ:
     Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
 
-import ctypes
 import gc
 import unittest
 
-from clang.cindex import AvailabilityKind
-from clang.cindex import CursorKind
-from clang.cindex import TemplateArgumentKind
-from clang.cindex import TranslationUnit
-from clang.cindex import TypeKind
-from clang.cindex import BinaryOperator
-from clang.cindex import StorageClass
-from .util import get_cursor
-from .util import get_cursors
-from .util import get_tu
-
+from .util import get_cursor, get_cursors, get_tu
 
 kInput = """\
 struct s0 {
@@ -170,7 +169,7 @@ def test_references(self):
         self.assertIsInstance(cursor.translation_unit, TranslationUnit)
 
         # If the TU was destroyed, this should cause a segfault.
-        parent = cursor.semantic_parent
+        cursor.semantic_parent
 
     def test_canonical(self):
         source = "struct X; struct X; struct X { int member; };"
@@ -344,7 +343,7 @@ class Bar {
         )
 
         self.assertEqual(len(copy_assignment_operators_cursors), 10)
-        self.assertTrue(len(non_copy_assignment_operators_cursors), 9)
+        self.assertEqual(len(non_copy_assignment_operators_cursors), 7)
 
         self.assertTrue(
             all(
@@ -923,7 +922,7 @@ def test_mangled_name(self):
         # and force the target. To enable this test to pass on all platforms, accept
         # all valid manglings.
         # [c-index-test handles this by running the source through clang, emitting
-        #  an AST file and running libclang on that AST file]
+        # an AST file and running libclang on that AST file]
         self.assertIn(
             foo.mangled_name, ("_Z3fooii", "__Z3fooii", "?foo@@YAHHH", "?foo@@YAHHH at Z")
         )
diff --git a/clang/bindings/python/tests/cindex/test_cursor_kind.py b/clang/bindings/python/tests/cindex/test_cursor_kind.py
index 87199dba06ed25..3b693ff45cfd44 100644
--- a/clang/bindings/python/tests/cindex/test_cursor_kind.py
+++ b/clang/bindings/python/tests/cindex/test_cursor_kind.py
@@ -1,11 +1,10 @@
 import os
-from clang.cindex import Config
+
+from clang.cindex import Config, CursorKind
 
 if "CLANG_LIBRARY_PATH" in os.environ:
     Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
 
-from clang.cindex import CursorKind
-
 import unittest
 
 
diff --git a/clang/bindings/python/tests/cindex/test_diagnostics.py b/clang/bindings/python/tests/cindex/test_diagnostics.py
index 041083d12c7f16..2ebd3414d692d8 100644
--- a/clang/bindings/python/tests/cindex/test_diagnostics.py
+++ b/clang/bindings/python/tests/cindex/test_diagnostics.py
@@ -1,14 +1,13 @@
 import os
-from clang.cindex import Config
+
+from clang.cindex import Config, Diagnostic
 
 if "CLANG_LIBRARY_PATH" in os.environ:
     Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
 
-from clang.cindex import *
-from .util import get_tu
-
 import unittest
 
+from .util import get_tu
 
 # FIXME: We need support for invalid translation units to test better.
 
diff --git a/clang/bindings/python/tests/cindex/test_enums.py b/clang/bindings/python/tests/cindex/test_enums.py
index 63b2292c5d9bdc..9e7f44fcf7867c 100644
--- a/clang/bindings/python/tests/cindex/test_enums.py
+++ b/clang/bindings/python/tests/cindex/test_enums.py
@@ -1,18 +1,18 @@
 import unittest
 
 from clang.cindex import (
-    TokenKind,
+    AccessSpecifier,
+    AvailabilityKind,
+    BinaryOperator,
     CursorKind,
-    TemplateArgumentKind,
     ExceptionSpecificationKind,
-    AvailabilityKind,
-    AccessSpecifier,
-    TypeKind,
-    RefQualifierKind,
     LinkageKind,
-    TLSKind,
+    RefQualifierKind,
     StorageClass,
-    BinaryOperator,
+    TemplateArgumentKind,
+    TLSKind,
+    TokenKind,
+    TypeKind,
 )
 
 
diff --git a/clang/bindings/python/tests/cindex/test_exception_specification_kind.py b/clang/bindings/python/tests/cindex/test_exception_specification_kind.py
index e4742db31adbe2..f7806ffad80124 100644
--- a/clang/bindings/python/tests/cindex/test_exception_specification_kind.py
+++ b/clang/bindings/python/tests/cindex/test_exception_specification_kind.py
@@ -1,18 +1,17 @@
 import os
-from clang.cindex import Config
+
+from clang.cindex import Config, CursorKind, ExceptionSpecificationKind
 
 if "CLANG_LIBRARY_PATH" in os.environ:
     Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
 
-import clang.cindex
-from clang.cindex import ExceptionSpecificationKind
-from .util import get_tu
-
 import unittest
 
+from .util import get_tu
+
 
 def find_function_declarations(node, declarations=[]):
-    if node.kind == clang.cindex.CursorKind.FUNCTION_DECL:
+    if node.kind == CursorKind.FUNCTION_DECL:
         declarations.append(node)
     for child in node.get_children():
         declarations = find_function_declarations(child, declarations)
diff --git a/clang/bindings/python/tests/cindex/test_file.py b/clang/bindings/python/tests/cindex/test_file.py
index 7024b0cdf11d97..14a3084ee2b47a 100644
--- a/clang/bindings/python/tests/cindex/test_file.py
+++ b/clang/bindings/python/tests/cindex/test_file.py
@@ -1,11 +1,10 @@
 import os
-from clang.cindex import Config
+
+from clang.cindex import Config, File, Index
 
 if "CLANG_LIBRARY_PATH" in os.environ:
     Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
 
-from clang.cindex import Index, File
-
 import unittest
 
 
diff --git a/clang/bindings/python/tests/cindex/test_index.py b/clang/bindings/python/tests/cindex/test_index.py
index bf29628f5e4e79..756f4bd9c7dfb5 100644
--- a/clang/bindings/python/tests/cindex/test_index.py
+++ b/clang/bindings/python/tests/cindex/test_index.py
@@ -1,20 +1,18 @@
 import os
-from clang.cindex import Config
+
+from clang.cindex import Config, Index, TranslationUnit
 
 if "CLANG_LIBRARY_PATH" in os.environ:
     Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
 
-from clang.cindex import *
-import os
 import unittest
 
-
 kInputsDir = os.path.join(os.path.dirname(__file__), "INPUTS")
 
 
 class TestIndex(unittest.TestCase):
     def test_create(self):
-        index = Index.create()
+        Index.create()
 
     # FIXME: test Index.read
 
diff --git a/clang/bindings/python/tests/cindex/test_linkage.py b/clang/bindings/python/tests/cindex/test_linkage.py
index 4a8838276fae57..93bf43a0420476 100644
--- a/clang/bindings/python/tests/cindex/test_linkage.py
+++ b/clang/bindings/python/tests/cindex/test_linkage.py
@@ -1,18 +1,14 @@
 import os
-from clang.cindex import Config
+
+from clang.cindex import Config, LinkageKind
 
 if "CLANG_LIBRARY_PATH" in os.environ:
     Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
 
-from clang.cindex import LinkageKind
-from clang.cindex import Cursor
-from clang.cindex import TranslationUnit
-
-from .util import get_cursor
-from .util import get_tu
-
 import unittest
 
+from .util import get_cursor, get_tu
+
 
 class TestLinkage(unittest.TestCase):
     def test_linkage(self):
diff --git a/clang/bindings/python/tests/cindex/test_location.py b/clang/bindings/python/tests/cindex/test_location.py
index 27854a312e6721..bbf79126ab1f8d 100644
--- a/clang/bindings/python/tests/cindex/test_location.py
+++ b/clang/bindings/python/tests/cindex/test_location.py
@@ -1,19 +1,20 @@
 import os
-from clang.cindex import Config
+
+from clang.cindex import (
+    Config,
+    Cursor,
+    File,
+    SourceLocation,
+    SourceRange,
+    TranslationUnit,
+)
 
 if "CLANG_LIBRARY_PATH" in os.environ:
     Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
 
-from clang.cindex import Cursor
-from clang.cindex import File
-from clang.cindex import SourceLocation
-from clang.cindex import SourceRange
-from clang.cindex import TranslationUnit
-from .util import get_cursor
-from .util import get_tu
-
 import unittest
 
+from .util import get_cursor, get_tu
 
 baseInput = "int one;\nint two;\n"
 
diff --git a/clang/bindings/python/tests/cindex/test_rewrite.py b/clang/bindings/python/tests/cindex/test_rewrite.py
index 42006f57554e28..6f7e5d906172c1 100644
--- a/clang/bindings/python/tests/cindex/test_rewrite.py
+++ b/clang/bindings/python/tests/cindex/test_rewrite.py
@@ -1,13 +1,7 @@
-import unittest
 import tempfile
+import unittest
 
-from clang.cindex import (
-    Rewriter,
-    TranslationUnit,
-    File,
-    SourceLocation,
-    SourceRange,
-)
+from clang.cindex import File, Rewriter, SourceLocation, SourceRange, TranslationUnit
 
 
 class TestRewrite(unittest.TestCase):
diff --git a/clang/bindings/python/tests/cindex/test_source_range.py b/clang/bindings/python/tests/cindex/test_source_range.py
index 47d8960fcafb35..81c0a9b05cff82 100644
--- a/clang/bindings/python/tests/cindex/test_source_range.py
+++ b/clang/bindings/python/tests/cindex/test_source_range.py
@@ -1,11 +1,11 @@
 import os
-from clang.cindex import Config
+
+from clang.cindex import Config, SourceLocation, SourceRange, TranslationUnit
 
 if "CLANG_LIBRARY_PATH" in os.environ:
     Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
 
 import unittest
-from clang.cindex import SourceLocation, SourceRange, TranslationUnit
 
 from .util import get_tu
 
diff --git a/clang/bindings/python/tests/cindex/test_tls_kind.py b/clang/bindings/python/tests/cindex/test_tls_kind.py
index b8ef74614ab038..f80a46f4d56806 100644
--- a/clang/bindings/python/tests/cindex/test_tls_kind.py
+++ b/clang/bindings/python/tests/cindex/test_tls_kind.py
@@ -1,18 +1,14 @@
 import os
-from clang.cindex import Config
+
+from clang.cindex import Config, TLSKind
 
 if "CLANG_LIBRARY_PATH" in os.environ:
     Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
 
-from clang.cindex import TLSKind
-from clang.cindex import Cursor
-from clang.cindex import TranslationUnit
-
-from .util import get_cursor
-from .util import get_tu
-
 import unittest
 
+from .util import get_cursor, get_tu
+
 
 class TestTLSKind(unittest.TestCase):
     def test_tls_kind(self):
diff --git a/clang/bindings/python/tests/cindex/test_token_kind.py b/clang/bindings/python/tests/cindex/test_token_kind.py
index 747d328a577dc4..594f30a448d84f 100644
--- a/clang/bindings/python/tests/cindex/test_token_kind.py
+++ b/clang/bindings/python/tests/cindex/test_token_kind.py
@@ -1,11 +1,10 @@
 import os
-from clang.cindex import Config
+
+from clang.cindex import Config, TokenKind
 
 if "CLANG_LIBRARY_PATH" in os.environ:
     Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
 
-from clang.cindex import TokenKind
-
 import unittest
 
 
diff --git a/clang/bindings/python/tests/cindex/test_tokens.py b/clang/bindings/python/tests/cindex/test_tokens.py
index 2cbf42c4c6cb93..b6c1fc8b836009 100644
--- a/clang/bindings/python/tests/cindex/test_tokens.py
+++ b/clang/bindings/python/tests/cindex/test_tokens.py
@@ -1,19 +1,14 @@
 import os
-from clang.cindex import Config
+
+from clang.cindex import Config, CursorKind, SourceLocation, SourceRange, TokenKind
 
 if "CLANG_LIBRARY_PATH" in os.environ:
     Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
 
-from clang.cindex import CursorKind
-from clang.cindex import Index
-from clang.cindex import SourceLocation
-from clang.cindex import SourceRange
-from clang.cindex import TokenKind
+import unittest
 
 from .util import get_tu
 
-import unittest
-
 
 class TestTokens(unittest.TestCase):
     def test_token_to_cursor(self):
diff --git a/clang/bindings/python/tests/cindex/test_translation_unit.py b/clang/bindings/python/tests/cindex/test_translation_unit.py
index ff7213c69dd0f1..56bf3742417554 100644
--- a/clang/bindings/python/tests/cindex/test_translation_unit.py
+++ b/clang/bindings/python/tests/cindex/test_translation_unit.py
@@ -1,30 +1,28 @@
 import os
-from clang.cindex import Config
+
+from clang.cindex import (
+    Config,
+    Cursor,
+    CursorKind,
+    File,
+    Index,
+    SourceLocation,
+    SourceRange,
+    TranslationUnit,
+    TranslationUnitLoadError,
+    TranslationUnitSaveError,
+)
 
 if "CLANG_LIBRARY_PATH" in os.environ:
     Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
 
-from contextlib import contextmanager
 import gc
-import os
-import sys
 import tempfile
 import unittest
+from contextlib import contextmanager
+from pathlib import Path
 
-from clang.cindex import CursorKind
-from clang.cindex import Cursor
-from clang.cindex import File
-from clang.cindex import Index
-from clang.cindex import SourceLocation
-from clang.cindex import SourceRange
-from clang.cindex import TranslationUnitSaveError
-from clang.cindex import TranslationUnitLoadError
-from clang.cindex import TranslationUnit
-from .util import get_cursor
-from .util import get_tu
-from .util import skip_if_no_fspath
-from .util import str_to_path
-
+from .util import get_cursor, get_tu
 
 kInputsDir = os.path.join(os.path.dirname(__file__), "INPUTS")
 
@@ -47,7 +45,7 @@ def save_tu_pathlike(tu):
     Returns the filename it was saved to.
     """
     with tempfile.NamedTemporaryFile() as t:
-        tu.save(str_to_path(t.name))
+        tu.save(Path(t.name))
         yield t.name
 
 
@@ -105,24 +103,21 @@ def test_unsaved_files(self):
         self.assertEqual(spellings[-1], "y")
 
     def test_unsaved_files_2(self):
-        if sys.version_info.major >= 3:
-            from io import StringIO
-        else:
-            from io import BytesIO as StringIO
+        from io import StringIO
+
         tu = TranslationUnit.from_source(
             "fake.c", unsaved_files=[("fake.c", StringIO("int x;"))]
         )
         spellings = [c.spelling for c in tu.cursor.get_children()]
         self.assertEqual(spellings[-1], "x")
 
-    @skip_if_no_fspath
     def test_from_source_accepts_pathlike(self):
         tu = TranslationUnit.from_source(
-            str_to_path("fake.c"),
+            Path("fake.c"),
             ["-Iincludes"],
             unsaved_files=[
                 (
-                    str_to_path("fake.c"),
+                    Path("fake.c"),
                     """
 #include "fake.h"
     int x;
@@ -130,7 +125,7 @@ def test_from_source_accepts_pathlike(self):
     """,
                 ),
                 (
-                    str_to_path("includes/fake.h"),
+                    Path("includes/fake.h"),
                     """
 #define SOME_DEFINE y
     """,
@@ -192,7 +187,6 @@ def test_save(self):
             self.assertTrue(os.path.exists(path))
             self.assertGreater(os.path.getsize(path), 0)
 
-    @skip_if_no_fspath
     def test_save_pathlike(self):
         """Ensure TranslationUnit.save() works with PathLike filename."""
 
@@ -234,14 +228,13 @@ def test_load(self):
             # Just in case there is an open file descriptor somewhere.
             del tu2
 
-    @skip_if_no_fspath
     def test_load_pathlike(self):
         """Ensure TranslationUnits can be constructed from saved files -
         PathLike variant."""
         tu = get_tu("int foo();")
         self.assertEqual(len(tu.diagnostics), 0)
         with save_tu(tu) as path:
-            tu2 = TranslationUnit.from_ast_file(filename=str_to_path(path))
+            tu2 = TranslationUnit.from_ast_file(filename=Path(path))
             self.assertEqual(len(tu2.diagnostics), 0)
 
             foo = get_cursor(tu2, "foo")
@@ -268,18 +261,17 @@ def test_get_file(self):
         with self.assertRaises(Exception):
             f = tu.get_file("foobar.cpp")
 
-    @skip_if_no_fspath
     def test_get_file_pathlike(self):
         """Ensure tu.get_file() works appropriately with PathLike filenames."""
 
         tu = get_tu("int foo();")
 
-        f = tu.get_file(str_to_path("t.c"))
+        f = tu.get_file(Path("t.c"))
         self.assertIsInstance(f, File)
         self.assertEqual(f.name, "t.c")
 
         with self.assertRaises(Exception):
-            f = tu.get_file(str_to_path("foobar.cpp"))
+            f = tu.get_file(Path("foobar.cpp"))
 
     def test_get_source_location(self):
         """Ensure tu.get_source_location() works."""
diff --git a/clang/bindings/python/tests/cindex/test_type.py b/clang/bindings/python/tests/cindex/test_type.py
index 928a9794e42133..ce05fdb1a1ebc0 100644
--- a/clang/bindings/python/tests/cindex/test_type.py
+++ b/clang/bindings/python/tests/cindex/test_type.py
@@ -1,5 +1,6 @@
 import os
-from clang.cindex import Config
+
+from clang.cindex import Config, CursorKind, RefQualifierKind, TranslationUnit, TypeKind
 
 if "CLANG_LIBRARY_PATH" in os.environ:
     Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
@@ -7,14 +8,7 @@
 import gc
 import unittest
 
-from clang.cindex import CursorKind
-from clang.cindex import TranslationUnit
-from clang.cindex import TypeKind
-from clang.cindex import RefQualifierKind
-from .util import get_cursor
-from .util import get_cursors
-from .util import get_tu
-
+from .util import get_cursor, get_cursors, get_tu
 
 kInput = """\
 
@@ -138,7 +132,7 @@ def test_references(self):
         self.assertIsInstance(t.translation_unit, TranslationUnit)
 
         # If the TU was destroyed, this should cause a segfault.
-        decl = t.get_declaration()
+        t.get_declaration()
 
     def testConstantArray(self):
         tu = get_tu(constarrayInput)
@@ -459,8 +453,8 @@ def test_offset(self):
             (["-target", "i386-pc-win32"], (8, 16, 0, 32, 64, 96)),
             (["-target", "msp430-none-none"], (2, 14, 0, 32, 64, 96)),
         ]
-        for flags, values in tries:
-            align, total, f1, bariton, foo, bar = values
+        for _, values in tries:
+            _, _, f1, bariton, foo, bar = values
             tu = get_tu(source)
             teststruct = get_cursor(tu, "Test")
             children = list(teststruct.get_children())
diff --git a/clang/bindings/python/tests/cindex/util.py b/clang/bindings/python/tests/cindex/util.py
index 8ba3114b35d1e1..5e66a9dc82c446 100644
--- a/clang/bindings/python/tests/cindex/util.py
+++ b/clang/bindings/python/tests/cindex/util.py
@@ -1,18 +1,6 @@
 # This file provides common utility functions for the test suite.
 
-import os
-
-HAS_FSPATH = hasattr(os, "fspath")
-
-if HAS_FSPATH:
-    from pathlib import Path as str_to_path
-else:
-    str_to_path = None
-
-import unittest
-
-from clang.cindex import Cursor
-from clang.cindex import TranslationUnit
+from clang.cindex import Cursor, TranslationUnit
 
 
 def get_tu(source, lang="c", all_warnings=False, flags=[]):
@@ -81,14 +69,8 @@ def get_cursors(source, spelling):
     return cursors
 
 
-skip_if_no_fspath = unittest.skipUnless(
-    HAS_FSPATH, "Requires file system path protocol / Python 3.6+"
-)
-
 __all__ = [
     "get_cursor",
     "get_cursors",
     "get_tu",
-    "skip_if_no_fspath",
-    "str_to_path",
 ]



More information about the cfe-commits mailing list