本文整理匯總了Python中typing.Literal方法的典型用法代碼示例。如果您正苦於以下問題:Python typing.Literal方法的具體用法?Python typing.Literal怎麽用?Python typing.Literal使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類typing
的用法示例。
在下文中一共展示了typing.Literal方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: open_bytes
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Literal [as 別名]
def open_bytes(self, mode='rb'):
# type: (Text) -> ContextManager[BinaryIO]
"""Return a context manager for opening the file in binary mode.
Args:
mode: The mode to open the file in. The "b" mode is implicit if not
already present. It must not have the "t" flag.
Returns:
Context manager that yields an open file.
Raises:
ValueError: if invalid inputs are provided.
"""
if 't' in mode:
raise ValueError('Invalid mode {!r}: "t" flag not allowed when opening '
'file in binary mode'.format(mode))
if 'b' not in mode:
mode += 'b'
cm = self._open(mode, encoding=None, errors=None) # type: ContextManager[BinaryIO]
return cm
# TODO(b/123775699): Once pytype supports typing.Literal, use overload and
# Literal to express more precise return types and remove the type comments in
# open_text and open_bytes.
示例2: request
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Literal [as 別名]
def request(
self, path: str,
args: ty.Sequence[str] = [], *,
opts: ty.Mapping[str, str] = {},
decoder: str = "none",
stream: bool = False,
offline: bool = False,
return_result: ty.Literal[False],
auth: auth_t = None,
cookies: cookies_t = None,
data: reqdata_sync_t = None,
headers: headers_t = None,
timeout: timeout_t = None
) -> None:
...
示例3: get_encoding
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Literal [as 別名]
def get_encoding(name: ty.Literal["none"]) -> Dummy:
...
示例4: __getitem__
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Literal [as 別名]
def __getitem__(cls, values):
if not isinstance(values, tuple):
values = (values,)
return type('Literal_', (Literal,), dict(__args__=values))
示例5: read_tsv
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Literal [as 別名]
def read_tsv(
path: Path, as_dict: Literal[False], **kwargs
) -> Generator[List[str], None, None]:
...
示例6: test_literal_type_typing
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Literal [as 別名]
def test_literal_type_typing(self):
self.flakes("""
from typing import Literal
def f(x: Literal['some string']) -> None:
return None
""")
示例7: test_literal_type_typing_extensions
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Literal [as 別名]
def test_literal_type_typing_extensions(self):
self.flakes("""
from typing_extensions import Literal
def f(x: Literal['some string']) -> None:
return None
""")
示例8: test_literal_type_some_other_module
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Literal [as 別名]
def test_literal_type_some_other_module(self):
"""err on the side of false-negatives for types named Literal"""
self.flakes("""
from my_module import compat
from my_module.compat import Literal
def f(x: compat.Literal['some string']) -> None:
return None
def g(x: Literal['some string']) -> None:
return None
""")
示例9: test_literal_union_type_typing
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Literal [as 別名]
def test_literal_union_type_typing(self):
self.flakes("""
from typing import Literal
def f(x: Literal['some string', 'foo bar']) -> None:
return None
""")
示例10: __getitem__
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Literal [as 別名]
def __getitem__(cls, values):
if not isinstance(values, tuple):
values = (values,)
return type("Literal_", (Literal,), dict(__args__=values))
示例11: test_is_literal_with_literal
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Literal [as 別名]
def test_is_literal_with_literal():
from typing import Literal
assert is_literal(Literal["A", "B"])
示例12: test_is_instance_with_literal_and_matching_type
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Literal [as 別名]
def test_is_instance_with_literal_and_matching_type():
from typing import Literal
assert is_instance("A", Literal["A", "B"])
示例13: test_is_instance_with_literal_and_not_matching_type
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Literal [as 別名]
def test_is_instance_with_literal_and_not_matching_type():
from typing import Literal
assert not is_instance("C", Literal["A", "B"])
示例14: test_is_instance_with_optional_literal_and_matching_type
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Literal [as 別名]
def test_is_instance_with_optional_literal_and_matching_type():
from typing import Literal
assert is_instance("A", Optional[Literal["A", "B"]])
示例15: test_is_instance_with_optional_literal_and_not_matching_type
# 需要導入模塊: import typing [as 別名]
# 或者: from typing import Literal [as 別名]
def test_is_instance_with_optional_literal_and_not_matching_type():
from typing import Literal
assert not is_instance("C", Optional[Literal["A", "B"]])