当前位置: 首页>>代码示例>>Python>>正文


Python parser.parse_string函数代码示例

本文整理汇总了Python中pytype.pytd.parse.parser.parse_string函数的典型用法代码示例。如果您正苦于以下问题:Python parse_string函数的具体用法?Python parse_string怎么用?Python parse_string使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了parse_string函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ParsePyTD

def ParsePyTD(src=None, filename=None, python_version=None, module=None,
              lookup_classes=False):
  """Parse pytd sourcecode and do name lookup for builtins.

  This loads a pytd and also makes sure that all names are resolved (i.e.,
  that all primitive types in the AST are ClassType, and not NameType).

  Args:
    src: PyTD source code.
    filename: The filename the source code is from.
    python_version: The Python version to parse the pytd for.
    module: The name of the module we're parsing.
    lookup_classes: If we should also lookup the class of every ClassType.

  Returns:
    A pytd.TypeDeclUnit.
  """
  assert python_version
  if src is None:
    with open(filename, "rb") as fi:
      src = fi.read()
  ast = parser.parse_string(src, filename=filename, name=module,
                            python_version=python_version)
  if module is not None:  # Allow "" as module name
    ast = ast.Visit(visitors.AddNamePrefix(ast.name + "."))
  if lookup_classes:
    ast = visitors.LookupClasses(ast, builtins.GetBuiltinsPyTD())
  return ast
开发者ID:tharvik,项目名称:pytype,代码行数:28,代码来源:utils.py

示例2: testOptional

 def testOptional(self):
   ast = parser.parse_string(textwrap.dedent("""
     def left(a: int) -> int
     def right(a: int, ...) -> int
   """))
   m = type_match.TypeMatch()
   self.assertEquals(m.match(ast.Lookup("left"), ast.Lookup("right"), {}),
                     booleq.TRUE)
开发者ID:dominichamon,项目名称:pytype,代码行数:8,代码来源:type_match_test.py

示例3: testGeneric

 def testGeneric(self):
   ast = parser.parse_string(textwrap.dedent("""
     class A<T>(nothing):
       pass
     left: A<?>
     right: A<?>
   """))
   ast = visitors.LookupClasses(ast)
   m = type_match.TypeMatch()
   self.assertEquals(m.match_type_against_type(
       ast.Lookup("left").type,
       ast.Lookup("right").type, {}), booleq.TRUE)
开发者ID:sfermigier,项目名称:pytype,代码行数:12,代码来源:type_match_test.py

示例4: testClassMatch

 def testClassMatch(self):
   ast = parser.parse_string(textwrap.dedent("""
     class Left():
       def method(self) -> ?
     class Right():
       def method(self) -> ?
       def method2(self) -> ?
   """))
   ast = visitors.LookupClasses(ast, self.mini_builtins)
   m = type_match.TypeMatch()
   left, right = ast.Lookup("Left"), ast.Lookup("Right")
   self.assertEquals(m.match(left, right, {}), booleq.TRUE)
   self.assertNotEquals(m.match(right, left, {}), booleq.TRUE)
开发者ID:dominichamon,项目名称:pytype,代码行数:13,代码来源:type_match_test.py

示例5: testExternal

 def testExternal(self):
   ast = parser.parse_string(textwrap.dedent("""
     class Base():
       pass
     class Foo(Base):
       pass
     base = ...  # type: Base
   """))
   ast = visitors.LookupClasses(ast, self.mini_builtins)
   m = type_match.TypeMatch(type_match.get_all_subclasses([ast]))
   mod1_foo = pytd.ExternalType("Foo", module="mod1", cls=ast.Lookup("Foo"))
   eq = m.match_type_against_type(mod1_foo, ast.Lookup("base").type, {})
   self.assertEquals(eq, booleq.TRUE)
开发者ID:dominichamon,项目名称:pytype,代码行数:13,代码来源:type_match_test.py

示例6: testGeneric

 def testGeneric(self):
   ast = parser.parse_string(textwrap.dedent("""
     T = TypeVar('T')
     class A(typing.Generic[T], object):
       pass
     left = ...  # type: A[?]
     right = ...  # type: A[?]
   """))
   ast = visitors.LookupClasses(ast, self.mini_builtins)
   m = type_match.TypeMatch()
   self.assertEquals(m.match_type_against_type(
       ast.Lookup("left").type,
       ast.Lookup("right").type, {}), booleq.TRUE)
开发者ID:dominichamon,项目名称:pytype,代码行数:13,代码来源:type_match_test.py

示例7: test_isinstance

 def test_isinstance(self):
   sourcecode = textwrap.dedent("""
     x = ...  # type: `~unknown1`
     def `~isinstance`(object: int, class_or_type_or_tuple: tuple[nothing]) -> `~unknown1`
     class `~unknown1`(object):
       pass
   """)
   expected = textwrap.dedent("""
     x = ...  # type: bool
   """).strip()
   ast = parser.parse_string(sourcecode)
   ast = convert_structural.convert_pytd(ast, self.builtins_pytd)
   self.assertMultiLineEqual(pytd.Print(ast), expected)
开发者ID:durin42,项目名称:pytype,代码行数:13,代码来源:convert_structural_test.py

示例8: testBaseClass

  def testBaseClass(self):
    ast = parser.parse_string(textwrap.dedent("""
      class Base():
        def f(self, x:Base) -> Base
      class Foo(Base):
        pass

      class Match():
        def f(self, x:Base) -> Base
    """))
    ast = visitors.LookupClasses(ast, self.mini_builtins)
    m = type_match.TypeMatch(type_match.get_all_subclasses([ast]))
    eq = m.match_Class_against_Class(ast.Lookup("Match"), ast.Lookup("Foo"), {})
    self.assertEquals(eq, booleq.TRUE)
开发者ID:dominichamon,项目名称:pytype,代码行数:14,代码来源:type_match_test.py

示例9: testExternal

 def testExternal(self):
   ast = parser.parse_string(textwrap.dedent("""
     class Base(nothing):
       pass
     class Foo(Base):
       pass
     base: Foo
     foo: Foo
   """))
   ast = visitors.LookupClasses(ast)
   m = type_match.TypeMatch({ast.Lookup("foo").type: [ast.Lookup("Base")]})
   mod1_foo = pytd.ExternalType("Foo", module="mod1", cls=ast.Lookup("Foo"))
   eq = m.match_type_against_type(mod1_foo, ast.Lookup("base").type, {})
   self.assertEquals(eq, booleq.TRUE)
开发者ID:pombredanne,项目名称:pytype,代码行数:14,代码来源:type_match_test.py

示例10: testSubclasses

 def testSubclasses(self):
   ast = parser.parse_string(textwrap.dedent("""
     class A():
       pass
     class B(A):
       pass
     a = ...  # type: A
     def left(a: B) -> B
     def right(a: A) -> A
   """))
   ast = visitors.LookupClasses(ast, self.mini_builtins)
   m = type_match.TypeMatch(type_match.get_all_subclasses([ast]))
   left, right = ast.Lookup("left"), ast.Lookup("right")
   self.assertEquals(m.match(left, right, {}), booleq.TRUE)
   self.assertNotEquals(m.match(right, left, {}), booleq.TRUE)
开发者ID:dominichamon,项目名称:pytype,代码行数:15,代码来源:type_match_test.py

示例11: testSubclasses

 def testSubclasses(self):
   ast = parser.parse_string(textwrap.dedent("""
     class A(nothing):
       pass
     class B(A):
       pass
     a : A
     def left(a: B) -> B
     def right(a: A) -> A
   """))
   ast = visitors.LookupClasses(ast)
   m = type_match.TypeMatch({ast.Lookup("a").type: [ast.Lookup("B")]})
   left, right = ast.Lookup("left"), ast.Lookup("right")
   self.assertEquals(m.match(left, right, {}), booleq.TRUE)
   self.assertNotEquals(m.match(right, left, {}), booleq.TRUE)
开发者ID:pombredanne,项目名称:pytype,代码行数:15,代码来源:type_match_test.py

示例12: test_convert

 def test_convert(self):
   sourcecode = textwrap.dedent("""
     class A(object):
         def foo(self, x: `~unknown1`) -> ?
         def foobaz(self, x: int) -> int
     class `~unknown1`(object):
         def foobaz(self, x: int) -> int
   """)
   expected = textwrap.dedent("""
     class A(object):
         def foo(self, x: A) -> Any: ...
         def foobaz(self, x: int) -> int: ...
   """).lstrip()
   ast = parser.parse_string(sourcecode)
   ast = convert_structural.convert_pytd(ast, self.builtins_pytd)
   self.assertMultiLineEqual(pytd.Print(ast), expected)
开发者ID:dominichamon,项目名称:pytype,代码行数:16,代码来源:convert_structural_test.py

示例13: _TestTypeParameters

 def _TestTypeParameters(self, reverse=False):
   ast = parser.parse_string(textwrap.dedent("""
     class `~unknown0`(nothing):
       def next(self) -> ?
     class A<T>(nothing):
       def next(self) -> ?
     class B(nothing):
       pass
     def left(x: `~unknown0`) -> ?
     def right(x: A<B>) -> ?
   """))
   ast = visitors.LookupClasses(ast)
   m = type_match.TypeMatch()
   left, right = ast.Lookup("left"), ast.Lookup("right")
   match = m.match(right, left, {}) if reverse else m.match(left, right, {})
   self.assertEquals(match, booleq.And((booleq.Eq("~unknown0", "A"),
                                        booleq.Eq("~unknown0.A.T", "B"))))
   self.assertIn("~unknown0.A.T", m.solver.variables)
开发者ID:sfermigier,项目名称:pytype,代码行数:18,代码来源:type_match_test.py

示例14: test_convert_with_type_params

  def test_convert_with_type_params(self):
    sourcecode = textwrap.dedent("""
      class A(object):
          def foo(self, x: `~unknown1`) -> bool

      class `~unknown1`():
          def __setitem__(self, _1: str, _2: `~unknown2`) -> ?
          def update(self, _1: NoneType or Dict[nothing, nothing]) -> ?

      class `~unknown2`():
          def append(self, v:NoneType) -> NoneType
    """)
    expected = textwrap.dedent("""
      class A(object):
          def foo(self, x: Dict[str, List[Any]]) -> bool: ...
    """).lstrip()
    ast = parser.parse_string(sourcecode)
    ast = convert_structural.convert_pytd(ast, self.builtins_pytd)
    self.assertMultiLineEqual(pytd.Print(ast), expected)
开发者ID:dominichamon,项目名称:pytype,代码行数:19,代码来源:convert_structural_test.py

示例15: _TestTypeParameters

 def _TestTypeParameters(self, reverse=False):
   ast = parser.parse_string(textwrap.dedent("""
     class `~unknown0`():
       def next(self) -> ?
     T = TypeVar('T')
     class A(typing.Generic[T], object):
       def next(self) -> ?
     class B():
       pass
     def left(x: `~unknown0`) -> ?
     def right(x: A[B]) -> ?
   """))
   ast = visitors.LookupClasses(ast, self.mini_builtins)
   m = type_match.TypeMatch()
   left, right = ast.Lookup("left"), ast.Lookup("right")
   match = m.match(right, left, {}) if reverse else m.match(left, right, {})
   self.assertEquals(match, booleq.And((booleq.Eq("~unknown0", "A"),
                                        booleq.Eq("~unknown0.A.T", "B"))))
   self.assertIn("~unknown0.A.T", m.solver.variables)
开发者ID:dominichamon,项目名称:pytype,代码行数:19,代码来源:type_match_test.py


注:本文中的pytype.pytd.parse.parser.parse_string函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。