コード例 #1
0
ファイル: solve.py プロジェクト: khukri/mypy-py
def solve_constraints( vars, constraints, basic):
    """Solve type constraints.

    Return lower bound for each type variable or None if the variable could
    not be solved.
    """
    # Collect a list of constraints for each type variable.
    cmap = {}
    for con in constraints:
        a = cmap.get(con.type_var, [])
        a.append(con)
        cmap[con.type_var] = a
    
    res = []

    # Solve each type variable separately.
    for tvar in vars:
        bottom = None
        top = None
        
        # Process each contraint separely, and calculate the lower and upper
        # bounds based on constraints. Note that we assume that the contraint
        # targets do not have contraint references.
        for c in cmap.get(tvar, []):
            if c.op == SUPERTYPE_OF:
                if bottom is None:
                    bottom = c.target
                else:
                    bottom = join_types(bottom, c.target, basic)
            else:
                if top is None:
                    top = c.target
                else:
                    top = meet_types(top, c.target, basic)
        
        if top is None:
            if isinstance(bottom, Void):
                top = Void()
            else:
                top = basic.object
        
        if bottom is None:
            if isinstance(top, Void):
                bottom = Void()
            else:
                bottom = NoneTyp()
        
        if isinstance(top, Any) or isinstance(bottom, Any):
            top = Any()
            bottom = Any()
        
        # Pick the most specific type if it satisfies the constraints.
        if (not top or not bottom or is_subtype(bottom, top)) and (
                not isinstance(top, ErrorType) and
                not isinstance(bottom, ErrorType)):
            res.append(bottom)
        else:
            res.append(None)
    
    return res
コード例 #2
0
ファイル: testtypes.py プロジェクト: ron-mypy/mypy
 def assert_simple_meet(self, s, t, meet):
     result = meet_types(s, t, self.fx.basic)
     actual = str(result)
     expected = str(meet)
     assert_equal(actual, expected,
                  'meet({}, {}) == {{}} ({{}} expected)'.format(s, t))
     if not isinstance(s, ErrorType) and not isinstance(result, ErrorType):
         assert_true(is_subtype(result, s),
                     '{} not subtype of {}'.format(result, s))
     if not isinstance(t, ErrorType) and not isinstance(result, ErrorType):
         assert_true(is_subtype(result, t),
                     '{} not subtype of {}'.format(result, t))
コード例 #3
0
ファイル: solve.py プロジェクト: gtreddy/mypy
 Typ top = None
 
 # Process each contraint separely, and calculate the lower and upper
 # bounds based on constraints. Note that we assume that the contraint
 # targets do not have contraint references.
 for c in cmap.get(tvar, []):
     if c.op == SUPERTYPE_OF:
         if bottom is None:
             bottom = c.target
         else:
             bottom = join_types(bottom, c.target, basic)
     else:
         if top is None:
             top = c.target
         else:
             top = meet_types(top, c.target, basic)
 
 if top is None:
     if isinstance(bottom, Void):
         top = Void()
     else:
         top = basic.object
 
 if bottom is None:
     if isinstance(top, Void):
         bottom = Void()
     else:
         bottom = NoneTyp()
 
 if isinstance(top, Any) or isinstance(bottom, Any):
     top = Any()