Python typing.TYPE_CHECKING Examples
The following are 30
code examples of typing.TYPE_CHECKING().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
typing
, or try the search function
.
Example #1
Source File: test_scavenging.py From vulture with MIT License | 6 votes |
def test_type_hint_comments(v): v.scan( """\ import typing if typing.TYPE_CHECKING: from typing import List, Text def foo(foo_li): # type: (List[Text]) -> None for bar in foo_li: bar.xyz() """ ) check(v.unused_imports, ["List", "Text"])
Example #2
Source File: instance_database.py From maubot with GNU Affero General Public License v3.0 | 6 votes |
def get_database(request: web.Request) -> web.Response: instance_id = request.match_info.get("id", "") instance = PluginInstance.get(instance_id, None) if not instance: return resp.instance_not_found elif not instance.inst_db: return resp.plugin_has_no_database if TYPE_CHECKING: table: Table column: Column return web.json_response({ table.name: { "columns": { column.name: { "type": str(column.type), "unique": column.unique or False, "default": column.default, "nullable": column.nullable, "primary": column.primary_key, "autoincrement": column.autoincrement, } for column in table.columns }, } for table in instance.get_db_tables().values() })
Example #3
Source File: _activity.py From ballistica with MIT License | 6 votes |
def _setup_player_and_team_types(self) -> None: """Pull player and team types from our typing.Generic params.""" # TODO: There are proper calls for pulling these in Python 3.8; # should update this code when we adopt that. # NOTE: If we get Any as PlayerType or TeamType (generally due # to no generic params being passed) we automatically use the # base class types, but also warn the user since this will mean # less type safety for that class. (its better to pass the base # player/team types explicitly vs. having them be Any) if not TYPE_CHECKING: self._playertype = type(self).__orig_bases__[-1].__args__[0] if not isinstance(self._playertype, type): self._playertype = Player print(f'ERROR: {type(self)} was not passed a Player' f' type argument; please explicitly pass ba.Player' f' if you do not want to override it.') self._teamtype = type(self).__orig_bases__[-1].__args__[1] if not isinstance(self._teamtype, type): self._teamtype = Team print(f'ERROR: {type(self)} was not passed a Team' f' type argument; please explicitly pass ba.Team' f' if you do not want to override it.') assert issubclass(self._playertype, Player) assert issubclass(self._teamtype, Team)
Example #4
Source File: pylintplugins.py From ballistica with MIT License | 6 votes |
def ignore_type_check_filter(node: nc.NodeNG) -> nc.NodeNG: """Ignore stuff under 'if TYPE_CHECKING:' block at module level.""" # Look for a non-nested 'if TYPE_CHECKING:' if (isinstance(node.test, astroid.Name) and node.test.name == 'TYPE_CHECKING' and isinstance(node.parent, astroid.Module)): # Find the module node. mnode = node while mnode.parent is not None: mnode = mnode.parent # First off, remove any names that are getting defined # in this block from the module locals. for cnode in node.body: _strip_import(cnode, mnode) # Now replace the body with a simple 'pass'. This will # keep pylint from complaining about grouped imports/etc. passnode = astroid.Pass(parent=node, lineno=node.lineno + 1, col_offset=node.col_offset + 1) node.body = [passnode] return node
Example #5
Source File: test_type_guide.py From dagster with Apache License 2.0 | 6 votes |
def test_mypy_compliance(): class EvenType: def __init__(self, num): assert num % 2 is 0 self.num = num if typing.TYPE_CHECKING: EvenDagsterType = EvenType else: EvenDagsterType = PythonObjectDagsterType(EvenType) @solid def double_even(_, even_num: EvenDagsterType) -> EvenDagsterType: return EvenType(even_num.num * 2) assert execute_solid(double_even, input_values={'even_num': EvenType(2)}).success
Example #6
Source File: sample.py From example-code-2e with MIT License | 6 votes |
def demo() -> None: import typing p1 = tuple(range(10)) s1 = sample(p1, 3) if typing.TYPE_CHECKING: reveal_type(p1) reveal_type(s1) print(p1) print(s1) p2 = 'The quick brown fox jumps over the lazy dog' s2 = sample(p2, 10) if typing.TYPE_CHECKING: reveal_type(p2) reveal_type(s2) print(p2) print(s2)
Example #7
Source File: demo_not_book.py From example-code-2e with MIT License | 6 votes |
def demo() -> None: NOT_BOOK_JSON = """ {"title": "Andromeda Strain", "flavor": "pistachio", "authors": true} """ not_book = from_json(NOT_BOOK_JSON) # <1> if TYPE_CHECKING: # <2> reveal_type(not_book) reveal_type(not_book['authors']) print(not_book) # <3> print(not_book['flavor']) # <4> xml = to_xml(not_book) # <5> print(xml) # <6>
Example #8
Source File: mymax_demo.py From example-code-2e with MIT License | 5 votes |
def demo_args_iter_not_comparable_with_key() -> None: args = [object(), object(), object()] key = id expected = max(args, key=id) result = my.max(args, key=key) print(args, key, expected, result, sep='\n') assert result == expected if TYPE_CHECKING: reveal_type(args) reveal_type(key) reveal_type(expected) reveal_type(result)
Example #9
Source File: mymax_demo.py From example-code-2e with MIT License | 5 votes |
def demo_args_list_float() -> None: args = [2.5, 3.5, 1.5] expected = 3.5 result = my.max(*args) print(args, expected, result, sep='\n') assert result == expected if TYPE_CHECKING: reveal_type(args) reveal_type(expected) reveal_type(result)
Example #10
Source File: mymax_demo.py From example-code-2e with MIT License | 5 votes |
def demo_empty_iterable_with_default() -> None: args: List[float] = [] default = None expected = None result = my.max(args, default=default) print(args, default, expected, result, sep='\n') assert result == expected if TYPE_CHECKING: reveal_type(args) reveal_type(default) reveal_type(expected) reveal_type(result)
Example #11
Source File: mymax_demo.py From example-code-2e with MIT License | 5 votes |
def demo_different_key_return_type() -> None: args = iter('banana kiwi mango apple'.split()) key = len expected = 'banana' result = my.max(args, key=key) print(args, key, expected, result, sep='\n') assert result == expected if TYPE_CHECKING: reveal_type(args) reveal_type(key) reveal_type(expected) reveal_type(result)
Example #12
Source File: mymax_demo.py From example-code-2e with MIT License | 5 votes |
def demo_different_key_none() -> None: args = iter('banana kiwi mango apple'.split()) key = None expected = 'mango' result = my.max(args, key=key) print(args, key, expected, result, sep='\n') assert result == expected if TYPE_CHECKING: reveal_type(args) reveal_type(key) reveal_type(expected) reveal_type(result) ###################################### intentional type errors
Example #13
Source File: double_test.py From example-code-2e with MIT License | 5 votes |
def test_double_str() -> None: given = 'A' result = double(given) assert result == given * 2 if TYPE_CHECKING: reveal_type(given) reveal_type(result)
Example #14
Source File: double_test.py From example-code-2e with MIT License | 5 votes |
def test_double_fraction() -> None: from fractions import Fraction given = Fraction(2, 5) result = double(given) assert result == given * 2 if TYPE_CHECKING: reveal_type(given) reveal_type(result)
Example #15
Source File: double_test.py From example-code-2e with MIT License | 5 votes |
def test_double_array() -> None: from array import array given = array('d', [1.0, 2.0, 3.14]) result = double(given) if TYPE_CHECKING: reveal_type(given) reveal_type(result)
Example #16
Source File: double_test.py From example-code-2e with MIT License | 5 votes |
def test_double_nparray() -> None: import numpy as np # type: ignore given = np.array([[1, 2], [3, 4]]) result = double(given) comparison = result == given * 2 assert comparison.all() if TYPE_CHECKING: reveal_type(given) reveal_type(result)
Example #17
Source File: arg_lab.py From example-code-2e with MIT License | 5 votes |
def f(a: str, *b: int, **c: float) -> None: if typing.TYPE_CHECKING: # reveal_type(b) reveal_type(c) print(a, b, c)
Example #18
Source File: ssh.py From tbot with GNU General Public License v3.0 | 5 votes |
def _connect(self) -> typing.Iterator[channel.Channel]: with self.host.clone() as h: authenticator = self.authenticator if isinstance(authenticator, auth.NoneAuthenticator): cmd = ["ssh", "-o", "BatchMode=yes"] elif isinstance(authenticator, auth.PrivateKeyAuthenticator): cmd = [ "ssh", "-o", "BatchMode=yes", "-i", authenticator.get_key_for_host(h), ] elif isinstance(authenticator, auth.PasswordAuthenticator): cmd = ["sshpass", "-p", authenticator.password, "ssh"] else: if typing.TYPE_CHECKING: authenticator._undefined_marker raise ValueError(f"Unknown authenticator {authenticator!r}") hk_disable = ( ["-o", "StrictHostKeyChecking=no"] if self.ignore_hostkey else [] ) cmd_str = h.escape( *cmd, *hk_disable, *["-p", str(self.port)], *[arg for opt in self.ssh_config for arg in ["-o", opt]], f"{self.username}@{self.hostname}", ) with tbot.log_event.command(h.name, cmd_str): h.ch.sendline(cmd_str + "; exit", read_back=True) yield h.ch.take()
Example #19
Source File: _field.py From ballistica with MIT License | 5 votes |
def __init__(self, d_key: str, valuetype: TC, store_default: bool = True) -> None: super().__init__(d_key) self.d_value = valuetype # This doesnt actually exist for us, but want the type-checker # to think it does (see TYPE_CHECKING note below). self.d_data: Any self._store_default = store_default
Example #20
Source File: _field.py From ballistica with MIT License | 5 votes |
def __init__(self, d_key: str, keytype: Type[TK], valuetype: TC, store_default: bool = True) -> None: super().__init__(d_key) self.d_value = valuetype # This doesnt actually exist for us, but want the type-checker # to think it does (see TYPE_CHECKING note below). self.d_data: Any self.d_keytype = keytype self._store_default = store_default # noinspection DuplicatedCode
Example #21
Source File: call.py From ballistica with MIT License | 5 votes |
def add(self, call: CT) -> None: """Add a callback to be run.""" print('Would add call', call) # Define Call() which can be used in type-checking call-wrappers that behave # similarly to functools.partial (in that they take a callable and some # positional arguments to be passed to it) # In type-checking land, We define several different _CallXArg classes # corresponding to different argument counts and define Call() as an # overloaded function which returns one of them based on how many args are # passed. # To use this, simply assign your call type to this Call for type checking: # Example: # class _MyCallWrapper: # <runtime class defined here> # if TYPE_CHECKING: # MyCallWrapper = bafoundation.executils.Call # else: # MyCallWrapper = _MyCallWrapper # Note that this setup currently only works with positional arguments; if you # would like to pass args via keyword you can wrap a lambda or local function # which takes keyword args and converts to a call containing keywords.
Example #22
Source File: __init__.py From ballistica with MIT License | 5 votes |
def get_files_hash(filenames: Sequence[Union[str, Path]], extrahash: str = '', int_only: bool = False, hashtype: Literal['md5', 'sha256'] = 'md5') -> str: """Return a md5 hash for the given files.""" import hashlib if not isinstance(filenames, list): raise Exception('expected a list') if TYPE_CHECKING: # Help Mypy infer the right type for this. hashobj = hashlib.md5() else: hashobj = getattr(hashlib, hashtype)() for fname in filenames: with open(fname, 'rb') as infile: while True: data = infile.read(2**20) if not data: break hashobj.update(data) hashobj.update(extrahash.encode()) if int_only: return str(int.from_bytes(hashobj.digest(), byteorder='big')) return hashobj.hexdigest()
Example #23
Source File: models.py From ormantic with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __init__(self, **data): if "pk" in data: data[self.Mapping.pk_name] = data.pop("pk") if typing.TYPE_CHECKING: self.__values__: Dict[str, Any] = {} self.__fields_set__: "SetStr" = set() pk_only = data.pop("__pk_only__", False) values, fields_set, _ = pydantic.validate_model( self, data, raise_exc=not pk_only ) object.__setattr__(self, "__values__", values) object.__setattr__(self, "__fields_set__", fields_set)
Example #24
Source File: conf.py From instaloader with MIT License | 5 votes |
def setup(app): typing.TYPE_CHECKING = True app.add_stylesheet("instaloader.css") app.connect('autodoc-process-signature', sphinx_autodoc_typehints.process_signature) app.connect('autodoc-process-docstring', sphinx_autodoc_typehints.process_docstring)
Example #25
Source File: hotword.py From ada with Apache License 2.0 | 5 votes |
def load(self) -> "Porcupine": """Load Porcupine object.""" dist = importlib_metadata.distribution("pvporcupine") porcupine_paths = [ f for f in cast(List[importlib_metadata.PackagePath], dist.files) if f.name == "porcupine.py" ] if not porcupine_paths: raise RuntimeError("Unable to find porcupine.py in pvporcupine package") porcupine_path = porcupine_paths[0].locate().parent lib_path = porcupine_path.parent.parent sys.path.append(str(porcupine_path)) if not TYPE_CHECKING: # pylint: disable=import-outside-toplevel, import-error from porcupine import Porcupine return Porcupine( library_path=str(self._library_path(lib_path)), model_file_path=str(self._model_file_path(lib_path)), keyword_file_path=str(self._keyword_file_path(lib_path)), sensitivity=0.5, )
Example #26
Source File: v2tls.py From ambassador with Apache License 2.0 | 5 votes |
def add_context(self, ctx: IRTLSContext) -> None: if TYPE_CHECKING: # This is needed because otherwise self.__setitem__ confuses things. handler: Callable[[str, str], None] if ctx.is_fallback: self.is_fallback = True for secretinfokey, handler, hkey in [ ( 'cert_chain_file', self.update_cert_zero, 'certificate_chain' ), ( 'private_key_file', self.update_cert_zero, 'private_key' ), ( 'cacert_chain_file', self.update_validation, 'trusted_ca' ), ]: if secretinfokey in ctx['secret_info']: handler(hkey, ctx['secret_info'][secretinfokey]) for ctxkey, handler, hkey in [ ( 'alpn_protocols', self.update_alpn, 'alpn_protocols' ), ( 'cert_required', self.__setitem__, 'require_client_certificate' ), ( 'min_tls_version', self.update_tls_version, 'tls_minimum_protocol_version' ), ( 'max_tls_version', self.update_tls_version, 'tls_maximum_protocol_version' ), ( 'sni', self.__setitem__, 'sni' ), ]: value = ctx.get(ctxkey, None) if value is not None: handler(hkey, value) # This is a separate loop because self.update_tls_cipher is not the same type # as the other updaters: it takes a list of strings for the value, not a single # string. Getting mypy to be happy with that is _annoying_. for ctxkey, list_handler, hkey in [ ( 'cipher_suites', self.update_tls_cipher, 'cipher_suites' ), ( 'ecdh_curves', self.update_tls_cipher, 'ecdh_curves' ), ]: value = ctx.get(ctxkey, None) if value is not None: list_handler(hkey, value)
Example #27
Source File: acresource.py From ambassador with Apache License 2.0 | 5 votes |
def from_resource(cls: Type[R], other: R, rkey: Optional[str]=None, location: Optional[str]=None, kind: Optional[str]=None, serialization: Optional[str]=None, name: Optional[str]=None, namespace: Optional[str]=None, metadata_labels: Optional[str] = None, apiVersion: Optional[str]=None, **kwargs) -> R: new_name = name or other.name new_apiVersion = apiVersion or other.apiVersion new_namespace = namespace or other.namespace new_metadata_labels = metadata_labels or other.get('metadata_labels', None) # mypy 0.730 is Just Flat Wrong here. It tries to be "more strict" about # super(), which is fine, but it also flags this particular super() call # as an error, even though Type[R] is necessarily a Resource type. # # Since it's complaining about "argument 2 for super is not an instance # of argument 1", we need to assert() isinstance here -- but of course, # cls is _not_ an instance at all, it's a class, so isinstance() will # fail at runtime. So we only do the assertion if TYPE_CHECKING. Grrrr. if TYPE_CHECKING: assert(isinstance(cls, Resource)) return super().from_resource(other, rkey=rkey, location=location, kind=kind, name=new_name, apiVersion=new_apiVersion, namespace=new_namespace, metadata_labels=new_metadata_labels, serialization=serialization, **kwargs) # ACResource.INTERNAL is the magic ACResource we use to represent something created by # Ambassador's internals.
Example #28
Source File: test_type_annotations.py From pyflakes with MIT License | 5 votes |
def test_idomiatic_typing_guards(self): # typing.TYPE_CHECKING: python3.5.3+ self.flakes(""" from typing import TYPE_CHECKING if TYPE_CHECKING: from t import T def f(): # type: () -> T pass """) # False: the old, more-compatible approach self.flakes(""" if False: from t import T def f(): # type: () -> T pass """) # some choose to assign a constant and do it that way self.flakes(""" MYPY = False if MYPY: from t import T def f(): # type: () -> T pass """)
Example #29
Source File: test_type_annotations.py From pyflakes with MIT License | 5 votes |
def test_typing_guard_for_protocol(self): self.flakes(""" from typing import TYPE_CHECKING if TYPE_CHECKING: from typing import Protocol else: Protocol = object class C(Protocol): def f(): # type: () -> int pass """)
Example #30
Source File: mymax_demo.py From example-code-2e with MIT License | 5 votes |
def demo_args_iter_str() -> None: args = iter('banana kiwi mango apple'.split()) expected = 'mango' result = my.max(args) print(args, expected, result, sep='\n') assert result == expected if TYPE_CHECKING: reveal_type(args) reveal_type(expected) reveal_type(result)