Example #1
0
 def visit_file(self, file_node: MypyFile, fnam: str, options: Options,
                patches: List[Callable[[], None]]) -> None:
     self.errors.set_file(fnam, file_node.fullname())
     self.options = options
     self.sem.options = options
     self.patches = patches
     self.is_typeshed_file = self.errors.is_typeshed_file(fnam)
     self.sem.globals = file_node.names
     with experiments.strict_optional_set(options.strict_optional):
         self.accept(file_node)
Example #2
0
 def visit_file(self, file_node: MypyFile, fnam: str, options: Options,
                patches: List[Callable[[], None]]) -> None:
     self.recurse_into_functions = True
     self.errors.set_file(fnam, file_node.fullname())
     self.options = options
     self.sem.options = options
     self.patches = patches
     self.is_typeshed_file = self.errors.is_typeshed_file(fnam)
     self.sem.cur_mod_id = file_node.fullname()
     self.sem.globals = file_node.names
     with experiments.strict_optional_set(options.strict_optional):
         self.accept(file_node)
Example #3
0
 def visit_file(self, file_node: MypyFile, fnam: str, options: Options,
                patches: List[Tuple[int, Callable[[], None]]]) -> None:
     self.recurse_into_functions = True
     self.errors.set_file(fnam, file_node.fullname())
     self.options = options
     self.sem.options = options
     self.patches = patches
     self.is_typeshed_file = self.errors.is_typeshed_file(fnam)
     self.sem.cur_mod_id = file_node.fullname()
     self.cur_mod_node = file_node
     self.sem.globals = file_node.names
     with experiments.strict_optional_set(options.strict_optional):
         self.scope.enter_file(file_node.fullname())
         self.accept(file_node)
         self.scope.leave()
Example #4
0
 def visit_file(self, file_node: MypyFile, fnam: str, options: Options,
                patches: List[Tuple[int, Callable[[], None]]]) -> None:
     self.recurse_into_functions = True
     self.errors.set_file(fnam, file_node.fullname(), scope=self.scope)
     self.options = options
     self.sem.options = options
     self.patches = patches
     self.is_typeshed_file = self.errors.is_typeshed_file(fnam)
     self.sem.cur_mod_id = file_node.fullname()
     self.cur_mod_node = file_node
     self.sem.globals = file_node.names
     with experiments.strict_optional_set(options.strict_optional):
         self.scope.enter_file(file_node.fullname())
         self.accept(file_node)
         self.analyze_symbol_table(file_node.names)
         self.scope.leave()
     del self.cur_mod_node
     self.patches = []
Example #5
0
    def visit_file(self, file: MypyFile, fnam: str, mod_id: str,
                   options: Options) -> None:
        """Perform the first analysis pass.

        Populate module global table.  Resolve the full names of
        definitions not nested within functions and construct type
        info structures, but do not resolve inter-definition
        references such as base classes.

        Also add implicit definitions such as __name__.

        In this phase we don't resolve imports. For 'from ... import',
        we generate dummy symbol table nodes for the imported names,
        and these will get resolved in later phases of semantic
        analysis.
        """
        sem = self.sem
        self.sem.options = options  # Needed because we sometimes call into it
        self.pyversion = options.python_version
        self.platform = options.platform
        sem.cur_mod_id = mod_id
        sem.cur_mod_node = file
        sem.errors.set_file(fnam, mod_id, scope=sem.scope)
        sem.globals = SymbolTable()
        sem.global_decls = [set()]
        sem.nonlocal_decls = [set()]
        sem.block_depth = [0]

        sem.scope.enter_file(mod_id)

        defs = file.defs

        with experiments.strict_optional_set(options.strict_optional):
            # Add implicit definitions of module '__name__' etc.
            for name, t in implicit_module_attrs.items():
                # unicode docstrings should be accepted in Python 2
                if name == '__doc__':
                    if self.pyversion >= (3, 0):
                        typ = UnboundType('__builtins__.str')  # type: Type
                    else:
                        typ = UnionType([
                            UnboundType('__builtins__.str'),
                            UnboundType('__builtins__.unicode')
                        ])
                else:
                    assert t is not None, 'type should be specified for {}'.format(
                        name)
                    typ = UnboundType(t)
                v = Var(name, typ)
                v._fullname = self.sem.qualified_name(name)
                self.sem.globals[name] = SymbolTableNode(GDEF, v)

            for d in defs:
                d.accept(self)

            # Add implicit definition of literals/keywords to builtins, as we
            # cannot define a variable with them explicitly.
            if mod_id == 'builtins':
                literal_types = [
                    ('None', NoneTyp()),
                    # reveal_type is a mypy-only function that gives an error with
                    # the type of its arg.
                    ('reveal_type', AnyType(TypeOfAny.special_form)),
                    # reveal_locals is a mypy-only function that gives an error with the types of
                    # locals
                    ('reveal_locals', AnyType(TypeOfAny.special_form)),
                ]  # type: List[Tuple[str, Type]]

                # TODO(ddfisher): This guard is only needed because mypy defines
                # fake builtins for its tests which often don't define bool.  If
                # mypy is fast enough that we no longer need those, this
                # conditional check should be removed.
                if 'bool' in self.sem.globals:
                    bool_type = self.sem.named_type('bool')
                    literal_types.extend([
                        ('True', bool_type),
                        ('False', bool_type),
                        ('__debug__', bool_type),
                    ])
                else:
                    # We are running tests without 'bool' in builtins.
                    # TODO: Find a permanent solution to this problem.
                    # Maybe add 'bool' to all fixtures?
                    literal_types.append(
                        ('True', AnyType(TypeOfAny.special_form)))

                for name, typ in literal_types:
                    v = Var(name, typ)
                    v._fullname = self.sem.qualified_name(name)
                    self.sem.globals[name] = SymbolTableNode(GDEF, v)

            del self.sem.options

        sem.scope.leave()
Example #6
0
    def visit_file(self, file: MypyFile, fnam: str, mod_id: str, options: Options) -> None:
        """Perform the first analysis pass.

        Populate module global table.  Resolve the full names of
        definitions not nested within functions and construct type
        info structures, but do not resolve inter-definition
        references such as base classes.

        Also add implicit definitions such as __name__.

        In this phase we don't resolve imports. For 'from ... import',
        we generate dummy symbol table nodes for the imported names,
        and these will get resolved in later phases of semantic
        analysis.
        """
        sem = self.sem
        self.sem.options = options  # Needed because we sometimes call into it
        self.pyversion = options.python_version
        self.platform = options.platform
        sem.cur_mod_id = mod_id
        sem.cur_mod_node = file
        sem.errors.set_file(fnam, mod_id, scope=sem.scope)
        sem.globals = SymbolTable()
        sem.global_decls = [set()]
        sem.nonlocal_decls = [set()]
        sem.block_depth = [0]

        sem.scope.enter_file(mod_id)

        defs = file.defs

        with experiments.strict_optional_set(options.strict_optional):
            # Add implicit definitions of module '__name__' etc.
            for name, t in implicit_module_attrs.items():
                # unicode docstrings should be accepted in Python 2
                if name == '__doc__':
                    if self.pyversion >= (3, 0):
                        typ = UnboundType('__builtins__.str')  # type: Type
                    else:
                        typ = UnionType([UnboundType('__builtins__.str'),
                                        UnboundType('__builtins__.unicode')])
                else:
                    assert t is not None, 'type should be specified for {}'.format(name)
                    typ = UnboundType(t)
                v = Var(name, typ)
                v._fullname = self.sem.qualified_name(name)
                self.sem.globals[name] = SymbolTableNode(GDEF, v)

            for d in defs:
                d.accept(self)

            # Add implicit definition of literals/keywords to builtins, as we
            # cannot define a variable with them explicitly.
            if mod_id == 'builtins':
                literal_types = [
                    ('None', NoneTyp()),
                    # reveal_type is a mypy-only function that gives an error with
                    # the type of its arg.
                    ('reveal_type', AnyType(TypeOfAny.special_form)),
                ]  # type: List[Tuple[str, Type]]

                # TODO(ddfisher): This guard is only needed because mypy defines
                # fake builtins for its tests which often don't define bool.  If
                # mypy is fast enough that we no longer need those, this
                # conditional check should be removed.
                if 'bool' in self.sem.globals:
                    bool_type = self.sem.named_type('bool')
                    literal_types.extend([
                        ('True', bool_type),
                        ('False', bool_type),
                        ('__debug__', bool_type),
                    ])
                else:
                    # We are running tests without 'bool' in builtins.
                    # TODO: Find a permanent solution to this problem.
                    # Maybe add 'bool' to all fixtures?
                    literal_types.append(('True', AnyType(TypeOfAny.special_form)))

                for name, typ in literal_types:
                    v = Var(name, typ)
                    v._fullname = self.sem.qualified_name(name)
                    self.sem.globals[name] = SymbolTableNode(GDEF, v)

            del self.sem.options

        sem.scope.leave()
Example #7
0
 def assert_meet_uninhabited(self, s: Type, t: Type) -> None:
     with strict_optional_set(False):
         self.assert_meet(s, t, self.fx.nonet)
     with strict_optional_set(True):
         self.assert_meet(s, t, self.fx.uninhabited)