Ejemplo n.º 1
0
def patch_class(clas: type, cfg: Config):
    if clas in GlobalPatchedList:
        return clas
    GlobalPatchedList.add(clas)

    try:
        ctx = DefaultCreationContext(
            typevars=dict(),
            declared_location=Location(
                file=inspect.getfile(clas),
                line_no=inspect.getsourcelines(clas)[1],
                line_span=len(inspect.getsourcelines(clas)[0]),
            ),
            checkedpkgprefixes=cfg.checkedprefixes)
    except (TypeError, OSError) as e:  # Built in types
        ctx = DefaultCreationContext(
            typevars=dict(),
            declared_location=Location(file="<not found>",
                                       line_no=0,
                                       line_span=1),
            checkedpkgprefixes=cfg.checkedprefixes,
        )

    setattr(clas, '__patched', True)

    is_protocol = hasattr(clas, 'mro') and Protocol in clas.mro()

    if hasattr(clas, '__class_getitem__') and not is_protocol:
        original = clas.__class_getitem__
        setattr(clas, '__class_getitem__',
                lambda *args: WrappedGenericAlias(original(*args), ctx))
Ejemplo n.º 2
0
    def wrap(self, err: UntypyTypeError) -> UntypyTypeError:
        (t, i) = err.next_type_and_indicator()

        return err.with_frame(
            Frame(t,
                  i,
                  declared=None,
                  responsable=Location(file="dummy", line_no=0, line_span=1)))
Ejemplo n.º 3
0
 def find_location(fn) -> Optional[Location]:
     fn = WrappedFunction.find_original(fn)
     try:
         return Location(
             file=inspect.getfile(fn),
             line_no=inspect.getsourcelines(fn)[1],
             source_line="".join(inspect.getsourcelines(fn)[0]),
         )
     except:  # Failes on builtins
         return None
 def responsable(self) -> Optional[Location]:
     try:
         if hasattr(self.iter, 'gi_frame'):
             return Location(
                 file=inspect.getfile(self.iter.gi_frame),
                 line_no=inspect.getsourcelines(self.iter.gi_frame)[1],
                 line_span=len(inspect.getsourcelines(self.iter.gi_frame)[0]),
             )
     except OSError:  # this call does not work all the time
         pass
     except TypeError:
         pass
     return None
Ejemplo n.º 5
0
def _find_bound_typevars(clas: type) -> (type, Dict[TypeVar, Any]):
    if not hasattr(clas, '__args__') or not hasattr(clas, '__origin__'):
        return (clas, dict())
    if not hasattr(clas.__origin__, '__parameters__'):
        return (clas, dict())

    keys = clas.__origin__.__parameters__
    values = clas.__args__

    if len(keys) != len(values):
        raise UntypyAttributeError(
            f"Some unbound Parameters in {clas.__name__}. "
            f"keys={keys} do not match values={values}.", [
                Location(file=inspect.getfile(clas),
                         line_no=inspect.getsourcelines(clas)[1],
                         source_line="".join(inspect.getsourcelines(clas)[0]))
            ])
    return (clas.__origin__, dict(zip(keys, values)))
Ejemplo n.º 6
0
 def __init__(self, typevars: dict[TypeVar, Any] = dict()):
     super().__init__(typevars.copy(),
                      Location(file="dummy", line_no=0, line_span=1),
                      checkedpkgprefixes=["test"])