• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python parser.parse_string函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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


示例16: testStrict

 def testStrict(self):
   ast = parser.parse_string(textwrap.dedent("""
     class list<T>(nothing):
       pass
     class A(nothing):
       pass
     class B(A):
       pass
     class `~unknown0`(nothing):
       pass
     a : A
     def left() -> `~unknown0`
     def right() -> list<A>
   """))
   ast = visitors.LookupClasses(ast)
   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.And((booleq.Eq("~unknown0", "list"),
                                 booleq.Eq("~unknown0.list.T", "A"))))
开发者ID:sfermigier,项目名称:pytype,代码行数:20,代码来源:type_match_test.py


示例17: testStrict

  def testStrict(self):
    ast = parser.parse_string(textwrap.dedent("""

      T = TypeVar('T')
      class list(typing.Generic[T], object):
        pass
      class A():
        pass
      class B(A):
        pass
      class `~unknown0`():
        pass
      a = ...  # type: A
      def left() -> `~unknown0`
      def right() -> list[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.And((booleq.Eq("~unknown0", "list"),
                                  booleq.Eq("~unknown0.list.T", "A"))))
开发者ID:dominichamon,项目名称:pytype,代码行数:22,代码来源:type_match_test.py


示例18: parse

 def parse(self, src):
   return parser.parse_string(textwrap.dedent(src))
开发者ID:durin42,项目名称:pytype,代码行数:2,代码来源:convert_structural_test.py


示例19: setUp

 def setUp(self):
   self.mini_builtins = parser.parse_string(textwrap.dedent("""
     class object:  # implicitly added by Generic
       pass
   """))
开发者ID:dominichamon,项目名称:pytype,代码行数:5,代码来源:type_match_test.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python pytz.country_timezones函数代码示例发布时间:2022-05-27
下一篇:
Python api.TVDB类代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap