示例#1
0
 def test_different_texts_are_not_equal(self):
     assert exceptions.SourceSyntaxError(
         filename="path/to/somefile.py",
         lineno=3,
         text="something wrong",
     ) != exceptions.SourceSyntaxError(
         filename="path/to/somefile.py",
         lineno=3,
         text="something else wrong",
     )
示例#2
0
 def test_same_values_are_equal(self):
     assert exceptions.SourceSyntaxError(
         filename="path/to/somefile.py",
         lineno=3,
         text="something wrong",
     ) == exceptions.SourceSyntaxError(
         filename="path/to/somefile.py",
         lineno=3,
         text="something wrong",
     )
示例#3
0
 def test_str(self):
     assert "Syntax error in path/to/somefile.py, line 3: something wrong" == str(
         exceptions.SourceSyntaxError(
             filename="path/to/somefile.py",
             lineno=3,
             text="something wrong",
         ))
示例#4
0
    def scan_for_imports(self, module: Module) -> Set[DirectImport]:
        """
        Note: this method only analyses the module in question and will not load any other
        code, so it relies on self.modules to deduce which modules it imports. (This is
        because you can't know whether "from foo.bar import baz" is importing a module
        called  `baz`, or a function `baz` from the module `bar`.)
        """
        direct_imports: Set[DirectImport] = set()

        module_filename = self._determine_module_filename(module)
        is_package = self._module_is_package(module_filename)
        module_contents = self._read_module_contents(module_filename)
        module_lines = module_contents.splitlines()
        try:
            ast_tree = ast.parse(module_contents)
        except SyntaxError as e:
            raise exceptions.SourceSyntaxError(
                filename=module_filename,
                lineno=e.lineno,
                text=e.text,
            )
        for node in ast.walk(ast_tree):
            direct_imports |= self._parse_direct_imports_from_node(
                node, module, module_lines, is_package
            )

        return direct_imports