### Install and Run Pytype Source: https://github.com/google/pytype/blob/main/docs/index.md Quickstart guide to install Pytype using pip and run it on a file or directory. ```shell pip install pytype pytype file_or_directory ``` -------------------------------- ### Install Pytype from Source Source: https://github.com/google/pytype/blob/main/docs/index.md Instructions to clone the Pytype repository from GitHub and install it using pip. ```shell git clone --recurse-submodules https://github.com/google/pytype.git cd pytype pip install . ``` -------------------------------- ### Install Ruby Source: https://github.com/google/pytype/blob/main/docs/developers/docs_debugging.md Install the full Ruby package if your current version is too old or non-existent. ```shell sudo apt-get install ruby-full ``` -------------------------------- ### Install Jekyll and Bundler Dependencies Source: https://github.com/google/pytype/blob/main/docs/developers/docs_debugging.md Install the necessary gems for Jekyll and Bundler, then update Jekyll and install project-specific dependencies. ```shell gem install jekyll bundler webrick gem update jekyll bundle install ``` -------------------------------- ### Install Pytype via Pip Source: https://github.com/google/pytype/blob/main/docs/index.md Standard installation of Pytype using pip, requiring wheel and setuptools. ```shell pip install pytype ``` -------------------------------- ### Install Pytype from Source (Alternative Submodule Update) Source: https://github.com/google/pytype/blob/main/docs/index.md Alternative method to update git submodules before installing Pytype from source. ```shell git submodule init git submodule update ``` -------------------------------- ### Example Module Imports Source: https://github.com/google/pytype/blob/main/docs/developers/type_stubs.md Illustrates how a module might import dependencies. ```python import dep1 from foo import dep2 ``` -------------------------------- ### Example Pytype Configuration File (TOML) Source: https://github.com/google/pytype/blob/main/docs/index.md An example TOML configuration file for Pytype. It demonstrates how to specify inputs, Python version, pythonpath, and disabled error types. Relative paths are resolved from the config file's location. ```toml $ cat ~/repo1/pytype.toml # NOTE: All relative paths are relative to the location of this file. [tool.pytype] # Space-separated list of files or directories to process. inputs = [ 'foo', ] # Python version (major.minor) of the target code. python_version = '3.9' # Paths to source code directories, separated by ':' pythonpath = .:~/repo2 # Space-separated list of error names to ignore. disable = [ 'attribute-error', ] ``` -------------------------------- ### Serve Documentation Locally Source: https://github.com/google/pytype/blob/main/docs/developers/docs_debugging.md Navigate to the docs directory and start the Jekyll development server with the --watch flag for live reloading. ```shell cd docs # you'll need to be in the pytype/docs directory bundle exec jekyll serve --watch ``` -------------------------------- ### Install Pytype Dependencies Source: https://github.com/google/pytype/blob/main/CONTRIBUTING.md Installs pinned versions of all required dependencies for Pytype. Ensure you have pip installed. ```shell pip install -r requirements.txt ``` -------------------------------- ### Install Pytype on WSL Source: https://github.com/google/pytype/blob/main/docs/index.md Installs Pytype on Windows Subsystem for Linux (WSL), including necessary system libraries. ```shell sudo apt install build-essential python3-dev libpython3-dev ``` -------------------------------- ### Instantiating with a Callable Factory Source: https://github.com/google/pytype/blob/main/docs/user_guide.md This example shows how to declare a function that accepts a callable object (a factory) which returns an integer. ```python from collections.abc import Callable def instantiate(factory: Callable[[], int]) -> int: return factory() ``` -------------------------------- ### Basic Python Module Source: https://github.com/google/pytype/blob/main/docs/developers/type_stubs.md Example of a simple Python module with a class and a function. ```python class Foo: CONSTANT = 42 def do_foo(x): return x ``` -------------------------------- ### Simple CFG Example Source: https://github.com/google/pytype/blob/main/docs/developers/typegraph.md Illustrates a basic control flow graph (CFG) with bindings and a query. Useful for understanding the initial state and goal resolution in the Type Graph solver. ```text n0 -> n1 -> n2 ``` -------------------------------- ### Backwards Reachability Cache Initialization Example Source: https://github.com/google/pytype/blob/main/docs/developers/typegraph.md Illustrates the initial state of the backwards reachability cache for a simple A -> B -> C node sequence. Each node can initially only reach itself. ```text reachable[A] = [True, False, False] reachable[B] = [False, True, False] reacahble[C] = [False, False, True] ``` -------------------------------- ### Variable Annotation Bytecode Source: https://github.com/google/pytype/blob/main/docs/developers/annotations.md The compiled bytecode for the variable annotation example, showing the `SETUP_ANNOTATIONS` and `STORE_ANNOTATION` opcodes. ```python SETUP_ANNOTATIONS ... class A definition ... LOAD_NAME 0 (A) STORE_ANNOTATION 1 (x) ``` -------------------------------- ### Python Code Example for CFG Source: https://github.com/google/pytype/blob/main/docs/developers/typegraph.md Illustrates a simple Python conditional statement that can be represented as a control flow graph. This example helps visualize program paths. ```python if some_val: # 1 x = 5 # 2 y = 6 # 3 else: x = "a" # 4 z = x.upper() + str(y) # 5 ``` -------------------------------- ### Install Pytype from Source Source: https://github.com/google/pytype/blob/main/CONTRIBUTING.md Installs Pytype in editable mode using pip, allowing executables to automatically pick up code edits. This method is useful for development but may limit logging from extension modules. ```shell pip install -e . ``` -------------------------------- ### Backwards Reachability Cache Update Example (C -> B) Source: https://github.com/google/pytype/blob/main/docs/developers/typegraph.md Shows the state of the backwards reachability cache after adding the C -> B edge, demonstrating how reachability propagates upwards. ```text reachable[A] = [True, False, False] reachable[B] = [False, True, False] reacahble[C] = [False, True, True] ``` -------------------------------- ### Check Ruby Version Source: https://github.com/google/pytype/blob/main/docs/developers/docs_debugging.md Verify that your Ruby installation is version 3.0.0 or greater. ```shell ruby -v # should be greater than 3.0.0 ``` -------------------------------- ### Reachability Check Example (is_reachable(30, 75)) Source: https://github.com/google/pytype/blob/main/docs/developers/typegraph.md Walks through the steps of checking reachability between node 30 and node 75 using the bucket and bit calculations. ```text 1. Find node 75's bucket: bucket = 75 >> 6 = 1. (This is equivalent to 75 / 64.) 1. Find node 75's bit: bit = 1 << (75 & 0x3F) = 1 << 11. 1. Check node 30's reachability: reachable[30][bucket] & bit. ``` -------------------------------- ### Complex CFG Example Source: https://github.com/google/pytype/blob/main/docs/developers/typegraph.md Demonstrates a more intricate CFG with conditional logic and multiple binding possibilities. Useful for understanding how the solver handles branching and potential contradictions. ```text a = 1 x0 b = 2 | \ c = True | \ if c: x1 | d = a + 2 | | else: | x2 d = a + b | / e = d + a x3 ``` -------------------------------- ### Enabling Pytype Errors After Disabling Source: https://github.com/google/pytype/blob/main/docs/user_guide.md This example shows how to re-enable Pytype warnings after they have been disabled for a section of the file. ```python # pytype: disable=attribute-error return self.socket.accept() # pytype: enable=attribute-error ``` -------------------------------- ### Example of `next(x)` compilation Source: https://github.com/google/pytype/blob/main/docs/developers/special_builtins.md Illustrates how the `next(x)` function call is compiled into bytecode, involving loading the global `next` and calling its `call` method. This highlights the need for pytype to implement special behavior when `next` is invoked. ```python LOAD_GLOBAL 0 (next) LOAD_FAST 0 (x) CALL_FUNCTION 1 ``` -------------------------------- ### Backwards Reachability Cache Update Example (B -> A) Source: https://github.com/google/pytype/blob/main/docs/developers/typegraph.md Depicts the final state of the backwards reachability cache after adding the B -> A edge, showing full reachability from C. ```text reachable[A] = [True, False, False] reachable[B] = [True, True, False] reacahble[C] = [True, True, True] ``` -------------------------------- ### Accessing Options Source: https://github.com/google/pytype/blob/main/docs/developers/config.md Access configuration options as attributes of the Options object. This example shows how to check the 'analyze_annotated' option. ```python if options.analyze_annotated: ... ``` -------------------------------- ### Postprocessor Option Storage Example Source: https://github.com/google/pytype/blob/main/docs/developers/config.md Illustrates how the Postprocessor class handles storing option values. If a specific _store_* method exists, it's used; otherwise, the input value is directly assigned. ```python if hasattr(postprocessor, '_store_foo'): output_options.foo = postprocessor._store_foo(input_options.foo) else: output_options.foo = input_options.foo ``` -------------------------------- ### Plain Python with a Bug Source: https://github.com/google/pytype/blob/main/docs/user_guide.md This example demonstrates a simple Python function with a bug that pytype can detect. The `str.join` method is used incorrectly. ```python def unannotated(x, y): return " + ".join(x, y) ``` -------------------------------- ### Attribute Checking Example Source: https://github.com/google/pytype/blob/main/docs/faq.md This example demonstrates how pytype correctly identifies a type error when an attribute is accessed on a type that does not support it. ```python (random.random() or 'foo').as_integer_ratio() ``` -------------------------------- ### Globally Disabling Pytype Errors Source: https://github.com/google/pytype/blob/main/docs/user_guide.md This example demonstrates how to disable all Pytype warnings for the entire file by adding a directive at the beginning. ```python # pytype: disable=attribute-error ``` -------------------------------- ### Function Accepting Union with None Source: https://github.com/google/pytype/blob/main/docs/user_guide.md This example shows a function that accepts a string or None as input, using the `|` operator for the type hint. ```python def greet(name: str | None) -> str: return 'Hi John Doe' if name is None else 'Hi ' + name ``` -------------------------------- ### Variable Annotation Example Source: https://github.com/google/pytype/blob/main/docs/developers/annotations.md A simple class definition and a variable annotation. This demonstrates the basic syntax for annotating a variable with a class type. ```python class A: pass x: A ``` -------------------------------- ### Corresponding Type Stub (.pyi) Source: https://github.com/google/pytype/blob/main/docs/developers/type_stubs.md Example of a type stub file (.pyi) for the basic Python module, describing types without implementation. ```python from typing import TypeVar T = TypeVar('T') class Foo: CONSTANT: int def do_foo(x: T) -> T: ... ``` -------------------------------- ### Generated Imports Info File Source: https://github.com/google/pytype/blob/main/docs/developers/type_stubs.md Example of an imports_info file generated by Pytype to map module paths to file paths for import lookup. ```text dep1 /home/.pytype/pyi/dep1.pyi foo/dep2 /home/.pytype/pyi/foo/dep2.pyi ``` -------------------------------- ### Pytype Matching Logic Example Source: https://github.com/google/pytype/blob/main/docs/developers/abstract_values.md Shows how Pytype uses the `matcher.match_var_against_type` function to verify if a value is compatible with an expected type. This is crucial for detecting errors like invalid function arguments. ```python matcher.match_var_against_type( Variable(Binding(PythonConstant(0))), PyTDClass(int)) ``` -------------------------------- ### Assert Type Mismatch Source: https://github.com/google/pytype/blob/main/docs/errors.md Example demonstrating the assert_type function with a type mismatch between the variable and the expected type. ```python x = 10 assert_type(x, str) ``` -------------------------------- ### Function Returning Union with None Source: https://github.com/google/pytype/blob/main/docs/user_guide.md This example demonstrates using the `|` operator (Python 3.10+) to specify that a function might return an integer or None. ```python from collections.abc import Sequence def find_index_of_name(sequence: Sequence[Any], name: str) -> int | None: try: return sequence.index(name) except ValueError: return None ``` -------------------------------- ### Extracting Keys from a Mapping Source: https://github.com/google/pytype/blob/main/docs/user_guide.md This example demonstrates using advanced typing constructs like `Mapping` and `Sequence` to declare a function that extracts keys from a mapping. ```python from collections.abc import Mapping, Sequence from typing import Any def keys(mapping: Mapping[str, Any]) -> Sequence[str]: return tuple(mapping) ``` -------------------------------- ### Conditional Execution Example Source: https://github.com/google/pytype/blob/main/docs/developers/typegraph.md Illustrates a Python program with conditional logic and its corresponding Control Flow Graph (CFG) representation. This helps visualize execution paths and how type information flows. ```python if some_val: # 1 x = 5 # 2 y = 6 # 3 else: x = "a" # 4 z = x.upper() + str(y) # 5 ``` ```text (1) / \ (2)<+ +>(4) | | v | (3)---+----+ | v (5) ``` -------------------------------- ### Pytype Argument Parser Setup Source: https://github.com/google/pytype/blob/main/docs/developers/config.md Defines the structure of pytype's command-line argument parser using argparse. This function is called internally by pytype to set up its options. ```python def make_parser(): o = argparse.ArgumentParser(...) add_basic_options(o) add_feature_flags(o) add_subtools(o) add_pickle_options(o) add_infrastructure_options(o) add_debug_options(o) return o ``` -------------------------------- ### Class with Late Initialization Source: https://github.com/google/pytype/blob/main/docs/user_guide.md This example defines a class that uses late initialization for a socket attribute. Pytype might flag this as an attribute error if not handled. ```python import socket class Server: def __init__(self, port): self.port = port def listen(self): self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.bind((socket.gethostname(), self.port)) self.socket.listen(backlog=5) def accept(self): return self.socket.accept() ``` -------------------------------- ### Setting a Native Slot for Property Get Source: https://github.com/google/pytype/blob/main/docs/developers/special_builtins.md Demonstrates how PropertyInstance sets the '__get__' slot to a custom implementation (fget_slot) to handle property access. ```Python class PropertyInstance(mixin.HasSlots, ...): def __init__(self, fget, ...): # sets the InterpreterFunction to call self.fget = fget # will be invoked when the target bytecode calls the property self.set_native_slot("__get__", self.fget_slot) def fget_slot(self, ...): return self.ctx.vm.call_function(self.fget, ...) ``` -------------------------------- ### PyTD Format for Dictionary Update Mutation Source: https://github.com/google/pytype/blob/main/docs/developers/type_stubs.md Example of PyTD format showing how mutations like dictionary updates are described by assignment to 'self'. ```python class dict(Dict[_K, _V]): def update(self, other: dict[_K2, _V2]) -> None: self = dict[_K | _K2, _V | _V2] ``` -------------------------------- ### Valid Super Call (Method With Self) Source: https://github.com/google/pytype/blob/main/docs/errors.md Provides a valid example of a super call within a method that correctly includes the `self` parameter. ```python class A(B): def f(self): super().foo() ``` -------------------------------- ### Pytype 'nothing' Type Example Source: https://github.com/google/pytype/blob/main/docs/faq.md Illustrates the 'nothing' type, which represents an unknown type in Pytype's inference, often seen with empty containers. ```python def f() -> str: return [] # [bad-return-type] # Expected: str # Actually returned: List[nothing] ``` -------------------------------- ### Annotated Python Function Source: https://github.com/google/pytype/blob/main/docs/user_guide.md This example shows a Python function with type annotations for parameters and the return value, following PEP 3107 and PEP 484. ```python def annotated(x: int, y: float = 0.0) -> int: return x + y ``` -------------------------------- ### Signature Mismatch in Overridden Method (Argument Order) Source: https://github.com/google/pytype/blob/main/docs/faq.md This example shows a signature mismatch due to differing argument order in overridden methods, causing TypeErrors during calls. ```python class A: def func(self, x: int, y: int) -> int: return 0 class B(A): def func(self, y: int, x: int) -> int: # signature-mismatch return x - y def f(a: A, x: int, y: int) -> None: print(a.func(x, y)) print(a.func(x=x, y=y)) print(a.func(x, y=y)) a = B() f(a, 2, 1) ``` -------------------------------- ### Inferring types with Pytype Source: https://github.com/google/pytype/blob/main/docs/index.md This example demonstrates how Pytype can detect type errors in Python code even without explicit type annotations, by inferring types. ```python def f(): return "PyCon" def g(): return f() + 2019 # pytype: line 4, in g: unsupported operand type(s) for +: 'str' # and 'int' [unsupported-operands] ``` -------------------------------- ### Dataclass with Type Annotations Source: https://github.com/google/pytype/blob/main/docs/developers/annotations.md Example of a Python dataclass using type annotations for its fields. This demonstrates a common use case for runtime annotation processing. ```python @dataclasses.dataclass class A: x: int y: str ``` -------------------------------- ### Example of cache-return Pragma Source: https://github.com/google/pytype/blob/main/docs/pragmas.md Associates a 'cache-return' pragma with a function definition. This pragma indicates that a function has no type-level side effects or dependence on inputs, allowing its return type to be cached. ```python def f(x: int) -> str: # pytype: pragma=cache-return ``` -------------------------------- ### Pytype Type Error Example Source: https://github.com/google/pytype/blob/main/docs/developers/abstract_values.md Illustrates how Pytype detects type errors based on annotations, unlike standard Python which treats them as comments. This example shows a violation of a `List[int]` annotation. ```python x: List[int] = [] x.append("hello") ``` -------------------------------- ### Running Pytype Tests Source: https://github.com/google/pytype/blob/main/pytype/tests/README.md This snippet shows how to set up a new test module in Pytype by including a call to `test_base.main()`. ```python if __name__ == "__main__": test_base.main() ``` -------------------------------- ### Pytype's Lenient Type Checking Source: https://github.com/google/pytype/blob/main/docs/index.md This example shows Pytype's lenient approach, allowing operations that succeed at runtime even if they might contradict strict type checking rules. ```python from typing import List def get_list() -> List[str]: lst = ["PyCon"] lst.append(2019) return [str(x) for x in lst] # mypy: line 4: error: Argument 1 to "append" of "list" has # incompatible type "int"; expected "str" ``` -------------------------------- ### Feature Not Supported Yet Source: https://github.com/google/pytype/blob/main/docs/errors.md This indicates that a specific feature or operation is not yet implemented in pytype. The example shows a workaround for calling a TypeGuard function with an arbitrary expression by using a local variable. ```python # Before: if foo(eggs[0]): do_something() # After: egg = eggs[0] if foo(egg): do_something() ``` -------------------------------- ### Bytecode for a 'for' Loop with Block Stack Management Source: https://github.com/google/pytype/blob/main/docs/developers/frames.md Demonstrates the bytecode structure for a 'for' loop, highlighting the paired SETUP_LOOP and POP_BLOCK opcodes used for block stack management. ```python SETUP_LOOP LOAD_FAST xs GET_ITER FOR_ITER STORE_FAST x ... code block ... POP_BLOCK ``` -------------------------------- ### Invalid List Annotation Source: https://github.com/google/pytype/blob/main/docs/errors.md This example demonstrates an invalid annotation for a list, where too many parameters are provided. ```python from typing import Union condition: bool = ... class _Foo: ... def Foo(): return _Foo() def f(x: list[int, str]): # bad: too many parameters for List pass ``` -------------------------------- ### Annotation Type Mismatch Source: https://github.com/google/pytype/blob/main/docs/errors.md Example of an annotation type mismatch where a variable is annotated as an integer but assigned a string. ```python x: int = 'hello world' ``` -------------------------------- ### Build Pytype Executables with Convenience Script Source: https://github.com/google/pytype/blob/main/CONTRIBUTING.md Builds Pytype executables using the convenience script, mimicking the process used in continuous integration tests. Executables are placed in the out/bin directory. ```shell python build_scripts/build.py ``` -------------------------------- ### Generate Pytype Configuration File Source: https://github.com/google/pytype/blob/main/docs/index.md Command to generate a sample Pytype configuration file. This file can then be customized for specific project needs. ```shell $ pytype --generate-config pytype.toml ``` -------------------------------- ### Invalid Ambiguous Type Annotation Source: https://github.com/google/pytype/blob/main/docs/errors.md This example illustrates an invalid type annotation that is ambiguous due to a conditional expression. ```python from typing import Union condition: bool = ... class _Foo: ... def Foo(): return _Foo() def f(x: int if condition else str): # bad: ambiguous type pass ``` -------------------------------- ### Assert Type with String Representation Source: https://github.com/google/pytype/blob/main/docs/errors.md Example of using assert_type with a string representation of a complex type to avoid importing it. ```python from collections.abc import Sequence assert_type(x, Sequence[int]) ``` ```python assert_type(x, 'Sequence[int]') ``` -------------------------------- ### Configure Pytype Build with CMake Source: https://github.com/google/pytype/blob/main/out/README.md Invoke CMake from the 'out' directory to configure the build. Use 'Ninja' as the generator for faster builds. ```bash cd out cmake ../ [-G Ninja] ``` -------------------------------- ### Create Options Programmatically Source: https://github.com/google/pytype/blob/main/docs/developers/config.md Create a config.Options object by passing keyword arguments to config.Options.create. This method allows direct programmatic configuration. ```python from pytype import config opts = config.Options.create(python_version=(3, 7), use_pickled_files=True) ``` -------------------------------- ### Build Version Info Method Source: https://github.com/google/pytype/blob/main/docs/developers/overlays.md The build_version_info method constructs the version tuple using the context's python_version attribute, converting each version component to a variable. ```python def build_version_info(ctx, module): [...] version = [] # major, minor for i in ctx.python_version: version.append(ctx.convert.constant_to_var(i)) [...] ``` -------------------------------- ### Invalid TypeVar Definition (Non-Type Constraints) Source: https://github.com/google/pytype/blob/main/docs/errors.md This example shows an invalid TypeVar definition using values that are not types as constraints. ```python from typing import TypeVar T = TypeVar("T", 0, 100) # bad: 0 and 100 are not types ``` -------------------------------- ### Define Initialization Library Source: https://github.com/google/pytype/blob/main/pytype/ast/CMakeLists.txt Defines the 'init' Python library, including the '__init__.py' source file. ```cmake py_library( NAME init SRCS __init__.py ) ``` -------------------------------- ### Silencing Specific Pytype Errors Source: https://github.com/google/pytype/blob/main/docs/user_guide.md This example shows how to silence a specific Pytype error, `attribute-error`, by adding a comment to the line. ```python return self.socket.accept() # pytype: disable=attribute-error ``` -------------------------------- ### TypedDict Assignment Mismatch Source: https://github.com/google/pytype/blob/main/docs/errors.md Example of an annotation type mismatch within a TypedDict assignment, where a value of the wrong type is assigned to a key. ```python from typing import TypedDict class A(TypedDict): x: int y: str a = A() a['x'] = '10' ``` -------------------------------- ### Configure Whole-Project Analysis Source: https://github.com/google/pytype/blob/main/docs/developers/process.md Add these flags to pytype-single when using whole-project analysis to specify the module name and import information. ```shell --module-name --imports_info .pytype/imports/.imports ``` -------------------------------- ### SysOverlay for sys.version_info Source: https://github.com/google/pytype/blob/main/docs/developers/overlays.md This overlay maps 'version_info' to a method that builds the version tuple, allowing pytype to load the actual value of sys.version_info for version checks. ```python class SysOverlay(overlay.Overlay): """A custom overlay for the 'sys' module.""" def __init__(self, ctx): member_map = { "version_info": build_version_info } [...] ``` -------------------------------- ### Run Instruction in Pytype VM Source: https://github.com/google/pytype/blob/main/docs/developers/main_loop.md This snippet shows the central dispatch point for opcode analysis within Pytype's virtual machine. It illustrates how each opcode is processed by a corresponding byte_* method. ```python state = run_instruction(op, state) ``` -------------------------------- ### Create Options from Command Line Arguments Source: https://github.com/google/pytype/blob/main/docs/developers/config.md Construct a config.Options object from command line flags, typically sys.argv. This is commonly used in single-file execution scripts. ```python import sys from pytype import config options = config.Options(sys.argv[1:], command_line=True) ``` -------------------------------- ### Boilerplate for a New Overlay Source: https://github.com/google/pytype/blob/main/docs/developers/overlays.md This is the basic structure for creating a new overlay for a module. It initializes the overlay with a member map and the module's AST. ```python from pytype.overlays import overlay class FooOverlay(overlay.Overlay): def __init__(self, ctx): member_map = {} ast = ctx.loader.import_name("foo") super().__init__(ctx, "foo", member_map, ast) ``` -------------------------------- ### Pytype Command-Line Usage Source: https://github.com/google/pytype/blob/main/docs/index.md Basic command-line syntax for running Pytype. Specifies positional arguments for input files or directories. ```shell usage: pytype [options] input [input ...] positional arguments: input file or directory to process ``` -------------------------------- ### Initialize Git Submodules Source: https://github.com/google/pytype/blob/main/CONTRIBUTING.md Ensures that all Git submodules within the Pytype repository are up to date. This is a prerequisite for building the pytype executable or running tests. ```shell git submodule update --init ``` -------------------------------- ### Enable Profiling for Single File Source: https://github.com/google/pytype/blob/main/docs/developers/process.md Pass this flag to pytype-single to enable profiling and specify the output path for the profile data. ```shell --profile ``` -------------------------------- ### Disabling bad-return-type Check for Empty Functions Source: https://github.com/google/pytype/blob/main/docs/errors.md This example shows how to use a block `disable` directive to suppress `bad-return-type` errors for empty functions with docstrings. ```python # pytype: disable=bad-return-type def f() -> int: """Override in subclasses and return int.""" # pytype: enable=bad-return-type ``` -------------------------------- ### Run CI Script for Pytype Source: https://github.com/google/pytype/blob/main/CONTRIBUTING.md Execute the CI script to perform linting and type-checking on the Pytype codebase. ```bash bash build_scripts/ci_script.sh ``` -------------------------------- ### Include Subdirectories Source: https://github.com/google/pytype/blob/main/pytype/tools/CMakeLists.txt Includes build configurations from various subdirectories. ```cmake add_subdirectory(analyze_project) ``` ```cmake add_subdirectory(annotate_ast) ``` ```cmake add_subdirectory(debugger) ``` ```cmake add_subdirectory(merge_pyi) ``` ```cmake add_subdirectory(traces) ``` -------------------------------- ### Define Visitor C Library Source: https://github.com/google/pytype/blob/main/pytype/ast/CMakeLists.txt Defines the 'visitor_c' Python library, including the 'visitor.py' source file. ```cmake py_library( NAME visitor_c SRCS visitor.py ) ``` -------------------------------- ### ParamSpec Error Source: https://github.com/google/pytype/blob/main/docs/errors.md This error relates to the incorrect usage of a parameter specification variable (ParamSpec). Examples include having only one ParamSpec in a signature or not adhering to the syntax for P.args and P.kwargs. -------------------------------- ### Declare Attribute for Pytype Awareness Source: https://github.com/google/pytype/blob/main/docs/errors.md Example showing how to declare an attribute's type at the class level to make pytype aware of its existence. This does not define the attribute at runtime. ```python class A: foo: int ``` -------------------------------- ### Define Visitor Library Source: https://github.com/google/pytype/blob/main/pytype/ast/CMakeLists.txt Defines the 'visitor' Python library, specifying its dependencies on '.init' and '.visitor_c'. ```cmake py_library( NAME visitor DEPS .init .visitor_c ) ``` -------------------------------- ### Create Output Binary Directory Source: https://github.com/google/pytype/blob/main/CMakeLists.txt Ensures that the directory specified by PYTYPE_OUT_BIN_DIR exists. This is where compiled binaries will be placed. ```cmake file(MAKE_DIRECTORY ${PYTYPE_OUT_BIN_DIR}) ``` -------------------------------- ### Signature Mismatch in Overridden Method (Missing Argument) Source: https://github.com/google/pytype/blob/main/docs/faq.md This example demonstrates a signature mismatch where a subclass method omits a required argument from the superclass method, leading to a TypeError. ```python class A: def func(self, x: int) -> int: return x class B(A): def func(self) -> int: # signature-mismatch return 0 def f(a: A, x: int) -> int: return a.func(x) a = B() f(a, 0) ``` -------------------------------- ### Postprocessor Module Name Storage with Dependencies Source: https://github.com/google/pytype/blob/main/docs/developers/config.md Demonstrates a postprocessing step for 'module_name' that depends on 'input' and 'pythonpath'. The @uses decorator ensures correct execution order. ```python @uses(["input", "pythonpath"]) def _store_module_name(self, module_name): if module_name is None: module_name = module_utils.get_module_name( self.output_options.input, self.output_options.pythonpath) self.output_options.module_name = module_name ``` -------------------------------- ### Configure Pytype for a Package Source: https://github.com/google/pytype/blob/main/docs/index.md Configuration for Pytype to check an entire package by adding settings to a pyproject.toml file. ```toml [tool.pytype] inputs = ['package_name'] ``` -------------------------------- ### Using type: ignore for Error Suppression Source: https://github.com/google/pytype/blob/main/docs/user_guide.md This example shows the use of the generic `type: ignore` comment for suppressing errors, which is less preferred than specific `pytype: disable` directives. ```python return self.socket.accept() # type: ignore ``` -------------------------------- ### Adding Python Path Option Source: https://github.com/google/pytype/blob/main/docs/developers/config.md Shows how the 'pythonpath' option is added using argparse and subsequently processed by the Postprocessor class to split the path string into a list. ```python o.add_argument( "-P", "--pythonpath", type=str, action="store", dest="pythonpath", default="", help="...") ``` ```python def _store_pythonpath(self, pythonpath): # Note that the below gives [""] for "", and ["x", ""] for "x:" # ("" is a valid entry to denote the current directory) self.output_options.pythonpath = pythonpath.split(os.pathsep) ``` -------------------------------- ### Define Python Test for CFG Utilities Source: https://github.com/google/pytype/blob/main/pytype/typegraph/CMakeLists.txt Sets up a Python test suite 'cfg_utils_test' that depends on the '.cfg' and '.cfg_utils' libraries. ```cmake py_test( NAME cfg_utils_test SRCS cfg_utils_test.py DEPS .cfg .cfg_utils ) ``` -------------------------------- ### Run Pytype Single with Verbosity Source: https://github.com/google/pytype/blob/main/CONTRIBUTING.md Executes the pytype-single tool with a specified logging verbosity level. Replace with the desired integer level and with the actual pytype arguments. ```shell out/bin/pytype-single -v ``` -------------------------------- ### Configure pytype_extensions Unit Test Source: https://github.com/google/pytype/blob/main/pytype_extensions/CMakeLists.txt Sets up a Python test for 'pytype_extensions'. It specifies the source file for the test and lists its dependencies, including other pytype modules. ```cmake py_test( NAME test_pytype_extensions SRCS test_pytype_extensions.py DEPS pytype.api pytype.pytd.api pytype.tests.test_base ) ``` -------------------------------- ### Pytype's Variable Bindings vs. CPython's Value Stack Source: https://github.com/google/pytype/blob/main/docs/developers/frames.md Illustrates how Pytype uses variables with multiple bindings to represent potential values during control flow analysis, unlike CPython's direct value stack. ```python if a: y = 10 else: y = None x = y ``` -------------------------------- ### Define Tuple Matching Behavior Source: https://github.com/google/pytype/blob/main/docs/developers/features.md Illustrates how to define matching rules for typing.Tuple, covering both value-to-annotation and annotation-to-value scenarios. ```python def f(x: X): ... f((a1, ..., a_n)) # what types X should this tuple match? ``` ```python def f(x: Tuple[A1, ..., A_n]): ... f(x) # what values x should match this tuple? ``` -------------------------------- ### Define Python Library: serde_utils_api Source: https://github.com/google/pytype/blob/main/pytype/imports/CMakeLists.txt Defines the 'serde_utils_api' Python library, listing its source file and dependencies. Note the overlap in source file with 'pickle_utils'. ```cmake py_library( NAME serde_utils_api SRCS pickle_utils.py DEPS pytype.pytd.pytd ) ``` -------------------------------- ### Print type annotations to stdout Source: https://github.com/google/pytype/blob/main/pytype/tools/merge_pyi/README.md Use this command to see how a Python file would look with type annotations from a stub file added, without modifying the original file. ```bash merge-pyi test_data/simple.py test_data/simple.pyi ``` -------------------------------- ### Infer Pyi with Single-File Analysis Source: https://github.com/google/pytype/blob/main/docs/developers/process.md Use pytype-single to infer and output a pyi file to standard output. ```shell pytype-single some_file.py -o - ``` -------------------------------- ### LOAD and STORE Operations in Bytecode Source: https://github.com/google/pytype/blob/main/docs/developers/frames.md Shows the typical bytecode compilation for a simple assignment statement involving LOAD and STORE operations. ```python x = a + b ``` ```python LOAD_FAST 0 (a) LOAD_FAST 1 (b) BINARY_ADD STORE_FAST 2 (x) ``` -------------------------------- ### Compile Python Source to Bytecode Source: https://github.com/google/pytype/blob/main/docs/developers/bytecode.md This snippet shows the logic for compiling Python source code to bytecode. If the host and target Python versions are the same, it uses a direct compilation function. If they differ, it writes the source to a temporary file, uses a target-specific Python executable to generate a .pyc file, and then reads the bytecode from it. ```python if host_version == target_version: bytecode = compile_source(src) else: write source to tmpfile src.py subprocess.call(target_python_exe, tmpfile) # generates src.pyc bytecode = read(src.pyc) ``` -------------------------------- ### Run Pytype Regression Tests Source: https://github.com/google/pytype/blob/main/docs/developers/python_version_upgrades.md Execute pytype's regression tests using the new Python version from the root of the cloned pytype repository. ```bash python build_scripts/run_tests.py ``` -------------------------------- ### Python 3.10+ Optional type syntax Source: https://github.com/google/pytype/blob/main/docs/typing_faq.md Illustrates the modern syntax for Optional types in Python 3.10 and later, using the union operator '|' for clarity. ```python x: Dict[str, Optional[str]] = {'a': 'hello', 'b': None} a = x.get('a') b = x.get('b') c = x.get('c') ``` -------------------------------- ### Check for Unresolved Dependencies with Pytype Source: https://github.com/google/pytype/blob/main/docs/index.md Command to run Pytype with a specific configuration file and input files, using the --unresolved flag to identify missing dependencies. ```shell $ pytype --config=~/repo1/pytype.toml ~/repo1/foo/*.py --unresolved ``` -------------------------------- ### Configure instrumentation_for_testing Unit Test Source: https://github.com/google/pytype/blob/main/pytype_extensions/CMakeLists.txt Configures a unit test for the 'instrumentation_for_testing' library. It lists the test source file and its dependencies, which include the 'instrumentation_for_testing' library itself and 'pytype_extensions'. ```cmake py_test( NAME instrumentation_for_testing_test SRCS instrumentation_for_testing_test.py DEPS .instrumentation_for_testing .pytype_extensions ) ``` -------------------------------- ### Pytype's byte_LOAD_FAST Implementation Source: https://github.com/google/pytype/blob/main/docs/developers/frames.md Details the Pytype implementation of the LOAD_FAST opcode, including variable lookup, error handling, and state management. ```python def byte_LOAD_FAST(self, state, op): """Load a local. Unlike LOAD_NAME, it doesn't fall back to globals.""" # op.arg is an index into the current frame's table of local variable names. # Use the index to look up the name of the variable to load. name = self.frame.f_code.co_varnames[op.arg] # returns 'a' # Load the variable 'a' in the current frame's `locals` dict. try: # load_local gets the current value of a in `val`. Note the common pattern # of threading an immutable state object through functions in vm.py by # having them take in a state and return an updated state. state, val = self.load_local(state, name) except KeyError: # Raise an error if we have referred to an undefined local variable val = self._name_error_or_late_annotation(name).to_variable(state.node) # Raise an error if we have referred to a deleted local variable. (We need # to do this because pytype stores deleted variables as a `Deleted` object # rather than removing them from the symbol table. self.check_for_deleted(state, name, val) # trace_opcode simply logs opcode execution. self.trace_opcode(op, name, val) # Push the value of a onto the data stack return state.push(val) ``` -------------------------------- ### Define Output Binary Directory Source: https://github.com/google/pytype/blob/main/CMakeLists.txt Sets the variable PYTYPE_OUT_BIN_DIR to the build's binary output directory. This is used for organizing build artifacts. ```cmake set(PYTYPE_OUT_BIN_DIR ${PROJECT_BINARY_DIR}/bin) ``` -------------------------------- ### Configure Ruby Gem Path Source: https://github.com/google/pytype/blob/main/docs/developers/docs_debugging.md Add environment variables to your .bashrc file to manage Ruby gems and ensure their executables are in your PATH. ```shell echo '# Install Ruby Gems to ~/gems' >> ~/.bashrc echo 'export GEM_HOME="$HOME/gems"' >> ~/.bashrc echo 'export PATH="$HOME/gems/bin:$PATH"' >> ~/.bashrc source ~/.bashrc ``` -------------------------------- ### Define Instances Library Source: https://github.com/google/pytype/blob/main/pytype/types/CMakeLists.txt Defines the 'instances' py_library, specifying its source file and dependency on the 'base' types library. This library likely deals with the representation of type instances. ```bazel py_library( NAME instances SRCS instances.py DEPS .base ) ``` -------------------------------- ### Enable Python Debugger Source: https://github.com/google/pytype/blob/main/docs/developers/process.md Insert this code snippet into your Python script to set a breakpoint and enter the pdb debugger at that point. ```python import pdb; pdb.set_trace() ``` -------------------------------- ### Run Single-File Analysis Source: https://github.com/google/pytype/blob/main/docs/developers/process.md Execute the pytype single-file analysis tool to check a Python file for type errors. ```shell pytype-single some_file.py ``` -------------------------------- ### _pyc Library Definition Source: https://github.com/google/pytype/blob/main/pytype/pyc/CMakeLists.txt Defines the internal _pyc library, listing its source file and dependencies. ```bazel py_library( NAME _pyc SRCS pyc.py DEPS .compiler pytype.utils ) ``` -------------------------------- ### Abstract Value Construction from Constants Source: https://github.com/google/pytype/blob/main/docs/developers/abstract_values.md Illustrates the conversion of Python constants into Pytype's abstract value representations using `Converter.constant_to_value`. Different constants map to different abstract types like `ConcreteValue` or `Instance`. ```python pyval = 'hello world' constant_to_value(pyval) ``` ```python pyval = 0 constant_to_value(pyval) ``` ```python pyval = 42 constant_to_value(pyval) ``` ```python pyval = pytd.Class(X) constant_to_value(pyval) ``` -------------------------------- ### Compiler Library Definition Source: https://github.com/google/pytype/blob/main/pytype/pyc/CMakeLists.txt Defines the compiler library, listing its source file and dependencies. ```bazel py_library( NAME compiler SRCS compiler.py DEPS .compile_bytecode pytype.utils pytype.platform_utils.platform_utils ) ``` -------------------------------- ### Define decorate PyTD library Source: https://github.com/google/pytype/blob/main/pytype/pytd/codegen/CMakeLists.txt Defines the 'decorate' Python library, listing its source file and dependencies. ```cmake py_library( NAME decorate SRCS decorate.py DEPS .function pytype.pytd.pytd_for_codegen ) ``` -------------------------------- ### Compile Bytecode Library Definition Source: https://github.com/google/pytype/blob/main/pytype/pyc/CMakeLists.txt Defines the compile_bytecode library, specifying its source file. ```bazel py_library( NAME compile_bytecode SRCS compile_bytecode.py ) ``` -------------------------------- ### Python VM Bytecode Execution Loop Source: https://github.com/google/pytype/blob/main/docs/developers/frames.md Simplified loop illustrating how bytecode is executed within a frame. It shows the process of iterating through blocks and opcodes, updating the state, and managing the frame. ```python def run_bytecode(code): frame = make_frame(code) push_frame(frame) for block in frame.f_code.order: state = initial state for block for op in block: state = self.run_instruction(op, state) pop_frame(frame) # does cleanup as well as removing the frame from the stack return frame.return_variable, frame.f_locals, frame.f_globals ``` -------------------------------- ### Run Pytype Tests Source: https://github.com/google/pytype/blob/main/CONTRIBUTING.md Use this script to run specific Pytype test targets. If no target is specified, all py_test and cc_test targets will be executed. ```shell python build_scripts/run_tests.py ``` -------------------------------- ### Python Bytecode for Static Method Definition Source: https://github.com/google/pytype/blob/main/docs/developers/special_builtins.md Illustrates the Python bytecode generated for defining a static method using the @staticmethod decorator. ```Python LOAD_NAME 3 (staticmethod) LOAD_CONST 1 () LOAD_CONST 2 ('A.f') MAKE_FUNCTION 0 CALL_FUNCTION 1 STORE_NAME 4 (f) ``` -------------------------------- ### Pseudocode for Abstract Value Modeling Source: https://github.com/google/pytype/blob/main/docs/developers/abstract_values.md This pseudocode demonstrates how Pytype models a Python class and its instance using abstract value objects. ```python # Create a "class" object for A obj1 = abstract.InterpreterClass( name = "A", bases = [builtinclass_object], members = {} ) # Create a "method" object for __init__, setting its containing class to A obj2 = abstract.Method( name = "__init__", containing_class = obj1, signature = (args=['x'], return=None) bytecode = ) # Fill in the member dictionary for class A # Note that we have no information about the type of A.x so we set it to the Any # type, which matches everything when type checked. obj1.members['__init__'] = obj2 obj1.members['x'] = builtinclass_Any # Create an "instance" object for foo obj3 = abstract.Instance( class = obj1, initializers = {'x': 10}, members = {} ) # Fill in the members for foo, based on the class and the initializer obj3.members['__init__'] = obj2 obj3.members['x'] = builtinclass_int # Fill in the variable name assignments globals = {'A': obj1, 'foo': obj3} ``` -------------------------------- ### Build Pytype Single in Debug Mode Source: https://github.com/google/pytype/blob/main/CONTRIBUTING.md Builds the pytype-single executable in debug mode using the build script. This enables logging from extension modules, which is typically unavailable in release builds. ```shell python build_scripts/build.py --debug ``` -------------------------------- ### Visiting a Class Node in Pytype AST Source: https://github.com/google/pytype/blob/main/docs/developers/type_stubs.md This Python code demonstrates a simplified `VisitClass` method within a Pytype visitor. It's used to generate a string representation of a class AST node, processing its bases and methods. ```python def VisitClass(self, node): bases_str = "(" + ", ".join(node.bases) + ")" header = ["class " + node.name + bases_str + ":"] method_lines = sum((m.splitlines() for m in node.methods), []) methods = [" " + m for m in method_lines] return "\n".join(header + methods) + "\n" ``` -------------------------------- ### Define Config Library Source: https://github.com/google/pytype/blob/main/pytype/tools/CMakeLists.txt Defines the 'config' Python library, including its source file and dependencies. ```cmake py_library( NAME config SRCS config.py DEPS pytype.platform_utils.platform_utils ) ``` -------------------------------- ### Define py_library for overlays Source: https://github.com/google/pytype/blob/main/pytype/rewrite/overlays/CMakeLists.txt Defines the main 'overlays' Python library, listing its dependencies. ```cmake py_library( NAME overlays DEPS ._overlays .enum_overlay .special_builtins ) ``` -------------------------------- ### Define Environment Library Source: https://github.com/google/pytype/blob/main/pytype/tools/CMakeLists.txt Defines the 'environment' Python library, listing its source file and dependencies. ```cmake py_library( NAME environment SRCS environment.py DEPS .runner pytype.imports.imports pytype.platform_utils.platform_utils ) ``` -------------------------------- ### Configure Target Properties for Windows and Unix-like Systems Source: https://github.com/google/pytype/blob/main/pytype/typegraph/CMakeLists.txt Sets specific target properties for the pytype.typegraph.cfg module to ensure correct library naming and suffixes on Windows and other platforms. ```cmake if (WIN32) set_target_properties( pytype.typegraph.cfg PROPERTIES PREFIX "" OUTPUT_NAME "cfg" ) else() set_target_properties( pytype.typegraph.cfg PROPERTIES PREFIX "" SUFFIX ".so" OUTPUT_NAME "cfg" ) endif() ``` -------------------------------- ### Type Variable Substitution in Pytype Source: https://github.com/google/pytype/blob/main/docs/developers/abstract_values.md Demonstrates how Pytype's matcher computes type parameter substitutions to validate generic function calls. It finds a mapping for type variables that satisfies the argument types. ```python T = TypeVar('T') def f(x: T, y: T): ... f(0, 1) ``` -------------------------------- ### Pyc Test Definition Source: https://github.com/google/pytype/blob/main/pytype/pyc/CMakeLists.txt Defines the pyc_test target, listing its source file and dependencies. ```bazel py_test( NAME pyc_test SRCS pyc_test.py DEPS ._pyc .compiler .opcodes pytype.tests.test_base ) ``` -------------------------------- ### Configure Shared CRT for Windows Source: https://github.com/google/pytype/blob/main/CMakeLists.txt On Windows, forces the use of the shared C Runtime Library for gtest. This ensures consistency in dynamic linking. ```cmake if (WIN32) set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) ENDIF() ``` -------------------------------- ### Set C++ Standard Source: https://github.com/google/pytype/blob/main/CMakeLists.txt Configures the C++ standard to C++20 for the project. This enables the use of modern C++ features. ```cmake set(CMAKE_CXX_STANDARD 20) ``` -------------------------------- ### Define C++ Test for Solver Functionality Source: https://github.com/google/pytype/blob/main/pytype/typegraph/CMakeLists.txt Configures a C++ test suite 'solver_test' with its source files and dependency on the '.typegraph' library. ```cmake cc_test( NAME solver_test SRCS solver_test.cc test_util.h DEPS .typegraph ) ``` -------------------------------- ### Compiler Test Definition Source: https://github.com/google/pytype/blob/main/pytype/pyc/CMakeLists.txt Defines the compiler_test target, specifying its source file and dependencies. ```bazel py_test( NAME compiler_test SRCS compiler_test.py DEPS .compiler ) ``` -------------------------------- ### Define Pytype AST Library Source: https://github.com/google/pytype/blob/main/pytype/ast/CMakeLists.txt Defines the 'ast' Python library, specifying its dependencies on '.debug' and '.visitor'. ```cmake py_library( NAME ast DEPS .debug .visitor ) ``` -------------------------------- ### Define Python Library for CFG Utilities Source: https://github.com/google/pytype/blob/main/pytype/typegraph/CMakeLists.txt Creates a Python library named 'cfg_utils' with specified source files and dependencies. This library provides utility functions for the CFG. ```cmake py_library( NAME cfg_utils SRCS cfg_utils.py DEPS .cfg ) ``` -------------------------------- ### VirtualMachine Interpretation of STORE_ATTR Source: https://github.com/google/pytype/blob/main/docs/developers/bytecode.md Shows how the VirtualMachine class interprets the STORE_ATTR opcode by using its argument to index into the code object's names list. ```python name = self.frame.f_code.co_names[op.arg] ```