Beispiel #1
0
def extract_django_settings_module(config_file_path: Optional[str]) -> str:
    errors = Errors()
    if config_file_path is None:
        errors.report(
            0, None,
            "'django_settings_module' is not set: no mypy config file specified"
        )
        errors.raise_error()

    parser = configparser.ConfigParser()
    parser.read(config_file_path)  # type: ignore

    if not parser.has_section('mypy.plugins.django-stubs'):
        errors.report(
            0,
            None,
            "'django_settings_module' is not set: no section [mypy.plugins.django-stubs]",
            file=config_file_path)
        errors.raise_error()
    if not parser.has_option('mypy.plugins.django-stubs',
                             'django_settings_module'):
        errors.report(
            0,
            None,
            "'django_settings_module' is not set: setting is not provided",
            file=config_file_path)
        errors.raise_error()

    django_settings_module = parser.get('mypy.plugins.django-stubs',
                                        'django_settings_module').strip('\'"')
    return django_settings_module
Beispiel #2
0
 def __init__(self, data_dir: str,
              lib_path: List[str],
              target: int,
              pyversion: Tuple[int, int],
              flags: List[str],
              ignore_prefix: str,
              custom_typing_module: str,
              reports: Reports) -> None:
     self.data_dir = data_dir
     self.errors = Errors()
     self.errors.set_ignore_prefix(ignore_prefix)
     self.lib_path = lib_path
     self.target = target
     self.pyversion = pyversion
     self.flags = flags
     self.custom_typing_module = custom_typing_module
     self.reports = reports
     self.semantic_analyzer = SemanticAnalyzer(lib_path, self.errors,
                                               pyversion=pyversion)
     modules = self.semantic_analyzer.modules
     self.semantic_analyzer_pass3 = ThirdPass(modules, self.errors)
     self.type_checker = TypeChecker(self.errors, modules, self.pyversion)
     self.states = []  # type: List[State]
     self.module_files = {}  # type: Dict[str, str]
     self.module_deps = {}  # type: Dict[Tuple[str, str], bool]
     self.missing_modules = set()  # type: Set[str]
Beispiel #3
0
def parse(
    source: Union[str, bytes],
    fnam: Optional[str] = None,
    errors: Optional[Errors] = None,
    options: Options = Options()) -> MypyFile:
    """Parse a source file, without doing any semantic analysis.

    Return the parse tree. If errors is not provided, raise ParseError
    on failure. Otherwise, use the errors object to report parse errors.
    """
    raise_on_error = False
    if errors is None:
        errors = Errors()
        raise_on_error = True
    errors.set_file('<input>' if fnam is None else fnam, None)
    is_stub_file = bool(fnam) and fnam.endswith('.pyi')
    try:
        assert options.python_version[0] < 3 and not is_stub_file
        ast = ast27.parse(source, fnam, 'exec')
        tree = ASTConverter(
            options=options,
            is_stub=is_stub_file,
            errors=errors,
        ).visit(ast)
        assert isinstance(tree, MypyFile)
        tree.path = fnam
        tree.is_stub = is_stub_file
    except SyntaxError as e:
        errors.report(e.lineno, e.offset, e.msg)
        tree = MypyFile([], [], False, set())

    if raise_on_error and errors.is_errors():
        errors.raise_error()

    return tree
Beispiel #4
0
 def __init__(self, data_dir: str,
              lib_path: List[str],
              target: int,
              output_dir: str,
              pyversion: int,
              flags: List[str],
              ignore_prefix: str) -> None:
     self.data_dir = data_dir
     self.errors = Errors()
     self.errors.set_ignore_prefix(ignore_prefix)
     self.lib_path = lib_path
     self.target = target
     self.output_dir = output_dir
     self.pyversion = pyversion
     self.flags = flags
     self.semantic_analyzer = SemanticAnalyzer(lib_path, self.errors)
     self.semantic_analyzer_pass3 = ThirdPass(self.errors)
     self.type_checker = TypeChecker(self.errors,
                                     self.semantic_analyzer.modules,
                                     self.pyversion)
     self.states = List[State]()
     self.module_files = Dict[str, str]()
     self.icode = Dict[str, FuncIcode]()
     self.binary_path = None # type: str
     self.module_deps = Dict[Tuple[str, str], bool]()
Beispiel #5
0
 def __init__(self, data_dir: str,
              lib_path: List[str],
              target: int,
              output_dir: str,
              pyversion: int,
              flags: List[str],
              ignore_prefix: str,
              custom_typing_module: str,
              html_report_dir: str) -> None:
     self.data_dir = data_dir
     self.errors = Errors()
     self.errors.set_ignore_prefix(ignore_prefix)
     self.lib_path = lib_path
     self.target = target
     self.output_dir = output_dir
     self.pyversion = pyversion
     self.flags = flags
     self.custom_typing_module = custom_typing_module
     self.html_report_dir = html_report_dir
     self.semantic_analyzer = SemanticAnalyzer(lib_path, self.errors,
                                               pyversion=pyversion)
     self.semantic_analyzer_pass3 = ThirdPass(self.errors)
     self.type_checker = TypeChecker(self.errors,
                                     self.semantic_analyzer.modules,
                                     self.pyversion)
     self.states = List[State]()
     self.module_files = Dict[str, str]()
     self.module_deps = Dict[Tuple[str, str], bool]()
     self.missing_modules = Set[str]()
Beispiel #6
0
def parse(source: Union[str, bytes], fnam: str = None, errors: Errors = None,
          pyversion: Tuple[int, int] = defaults.PYTHON3_VERSION,
          custom_typing_module: str = None) -> MypyFile:
    """Parse a source file, without doing any semantic analysis.

    Return the parse tree. If errors is not provided, raise ParseError
    on failure. Otherwise, use the errors object to report parse errors.

    The pyversion (major, minor) argument determines the Python syntax variant.
    """
    raise_on_error = False
    if errors is None:
        errors = Errors()
        raise_on_error = True
    errors.set_file('<input>' if fnam is None else fnam)
    is_stub_file = bool(fnam) and fnam.endswith('.pyi')
    try:
        assert pyversion[0] < 3 and not is_stub_file
        ast = ast27.parse(source, fnam, 'exec')
        tree = ASTConverter(pyversion=pyversion,
                            is_stub=is_stub_file,
                            errors=errors,
                            custom_typing_module=custom_typing_module,
                            ).visit(ast)
        assert isinstance(tree, MypyFile)
        tree.path = fnam
        tree.is_stub = is_stub_file
    except SyntaxError as e:
        errors.report(e.lineno, e.offset, e.msg)
        tree = MypyFile([], [], False, set())

    if raise_on_error and errors.is_errors():
        errors.raise_error()

    return tree
Beispiel #7
0
 def _make_manager(self) -> BuildManager:
     errors = Errors()
     options = Options()
     manager = BuildManager(
         data_dir='',
         lib_path=[],
         ignore_prefix='',
         source_set=BuildSourceSet([]),
         reports=Reports('', {}),
         options=options,
         version_id=__version__,
         plugin=Plugin(options),
         errors=errors,
     )
     return manager
Beispiel #8
0
 def _make_manager(self) -> BuildManager:
     errors = Errors()
     options = Options()
     fscache = FileSystemCache()
     manager = BuildManager(
         data_dir='',
         lib_path=[],
         ignore_prefix='',
         source_set=BuildSourceSet([]),
         reports=Reports('', {}),
         options=options,
         version_id=__version__,
         plugin=Plugin(options),
         errors=errors,
         flush_errors=lambda msgs, serious: None,
         fscache=fscache,
     )
     return manager
Beispiel #9
0
def parse(source: Union[str, bytes],
          fnam: str,
          module: Optional[str],
          errors: Optional[Errors] = None,
          options: Optional[Options] = None) -> MypyFile:
    """Parse a source file, without doing any semantic analysis.

    Return the parse tree. If errors is not provided, raise ParseError
    on failure. Otherwise, use the errors object to report parse errors.
    """
    raise_on_error = False
    if errors is None:
        errors = Errors()
        raise_on_error = True
    if options is None:
        options = Options()
    errors.set_file(fnam, module)
    is_stub_file = fnam.endswith('.pyi')
    try:
        if is_stub_file:
            feature_version = defaults.PYTHON3_VERSION[1]
        else:
            assert options.python_version[0] >= 3
            feature_version = options.python_version[1]
        ast = ast3.parse(source, fnam, 'exec', feature_version=feature_version)

        tree = ASTConverter(
            options=options,
            is_stub=is_stub_file,
            errors=errors,
        ).visit(ast)
        tree.path = fnam
        tree.is_stub = is_stub_file
    except SyntaxError as e:
        errors.reportErrorCode(errorcode.SYNTAX_ERROR(e.msg),
                               e.lineno,
                               e.offset,
                               blocker=True)
        tree = MypyFile([], [], False, set())

    if raise_on_error and errors.is_errors():
        errors.raise_error()

    return tree
Beispiel #10
0
def parse(source: Union[str, bytes],
          fnam: str,
          module: Optional[str],
          errors: Optional[Errors] = None,
          options: Optional[Options] = None) -> MypyFile:
    """Parse a source file, without doing any semantic analysis.

    Return the parse tree. If errors is not provided, raise ParseError
    on failure. Otherwise, use the errors object to report parse errors.
    """
    raise_on_error = False
    if errors is None:
        errors = Errors()
        raise_on_error = True
    if options is None:
        options = Options()
    errors.set_file(fnam, module)
    is_stub_file = fnam.endswith('.pyi')
    try:
        assert options.python_version[0] < 3 and not is_stub_file
        # Disable deprecation warnings about <>.
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=DeprecationWarning)
            ast = ast27.parse(source, fnam, 'exec')
        tree = ASTConverter(
            options=options,
            errors=errors,
        ).visit(ast)
        assert isinstance(tree, MypyFile)
        tree.path = fnam
        tree.is_stub = is_stub_file
    except SyntaxError as e:
        errors.report(e.lineno if e.lineno is not None else -1,
                      e.offset,
                      e.msg,
                      blocker=True,
                      code=codes.SYNTAX)
        tree = MypyFile([], [], False, {})

    if raise_on_error and errors.is_errors():
        errors.raise_error()

    return tree
Beispiel #11
0
 def _make_manager(self) -> BuildManager:
     errors = Errors()
     options = Options()
     fscache = FileSystemCache()
     search_paths = SearchPaths((), (), (), ())
     manager = BuildManager(
         data_dir='',
         search_paths=search_paths,
         ignore_prefix='',
         source_set=BuildSourceSet([]),
         reports=Reports('', {}),
         options=options,
         version_id=__version__,
         plugin=Plugin(options),
         plugins_snapshot={},
         errors=errors,
         flush_errors=lambda msgs, serious: None,
         fscache=fscache,
         stdout=sys.stdout,
         stderr=sys.stderr,
     )
     return manager
Beispiel #12
0
def tokenizer_format_call(
        format_str: str) -> Optional[Tuple[List[str], List[FormatOp]]]:
    """Tokenize a str.format() format string.

    The core function parse_format_value() is shared with mypy.
    With these specifiers, we then parse the literal substrings
    of the original format string and convert `ConversionSpecifier`
    to `FormatOp`.

    Return:
        A list of string literals and a list of FormatOps. The literals
        are interleaved with FormatOps and the length of returned literals
        should be exactly one more than FormatOps.
        Return None if it cannot parse the string.
    """
    # Creates an empty MessageBuilder here.
    # It wouldn't be used since the code has passed the type-checking.
    specifiers = parse_format_value(format_str, EMPTY_CONTEXT,
                                    MessageBuilder(Errors(), {}))
    if specifiers is None:
        return None
    format_ops = generate_format_ops(specifiers)
    if format_ops is None:
        return None

    literals: List[str] = []
    last_end = 0
    for spec in specifiers:
        # Skip { and }
        literals.append(format_str[last_end:spec.start_pos - 1])
        last_end = spec.start_pos + len(spec.whole_seq) + 1
    literals.append(format_str[last_end:])
    # Deal with escaped {{
    literals = [x.replace('{{', '{').replace('}}', '}') for x in literals]

    return literals, format_ops
Beispiel #13
0
def temp_message_builder() -> MessageBuilder:
    """Return a message builder usable for throwaway errors (which may not format properly)."""
    return MessageBuilder(Errors(), {})
Beispiel #14
0
def temp_message_builder() -> MessageBuilder:
    """Return a message builder usable for collecting errors locally."""
    return MessageBuilder(Errors())