Development Hints
Deterministic behavior
Pynguin’s behavior is inherently probabilistic. However, in order to easily reproduce an execution, e.g. for an evaluation or for debugging purposes, it must be possible to achieve deterministic behavior.
For this purpose, whenever an implementation uses randomness, it must be done via pynguin.utils.randomness.
This module contains a singleton instance of a (pseudo) random-number generator that is seeded at startup (using --seed).
Furthermore, when an implementation’s behavior depends on the iteration order of a data structure, e.g., when picking a random element from it, one has to ensure that this order is deterministic:
lists and tuples are ordered by design.
dicts store their insertion order beginning with Python 3.7 and are therefore safe to use.
Python’s builtin sets do not guarantee any order, thus one has to use
OrderedSet, which is a set implementation that stores the insertion order of its elements.
Overriding __eq__ and __hash__ methods
Similar to the Java world, we enforce to adhere to contracts for equals and hash
methods as described, for example, in the Java API documentation.
More information can also be found in Joshua Bloch’s famous book Effective Java.
In particular, when you override the __eq__ or the __hash__ method of a class
you are also required to override its opponent.
The following contracts should hold (adopted from the Java API documentation):
For
__hash__
__hash__must consistently return the same integer whenever it is invoked on the same object more than once during one execution of the Python application, provided no information used in__eq__comparisons on the same object is modified. The integer need not remain consistent from one application execution to another execution of the same application.If two objects are equal according to the
__eq__method then the__hash__value of the two objects must be the sameIt is not required that for two objects being unequal according to the
__eq__method, the__hash__value must be distinct, although this could improve performance of hash tables.
For
__eq__
The relation is reflexive: for any non-null reference value
x,x.__eq__(x)should returnTrueThe relation is symmetric: for any non-null reference values
xandy,x.__eq__(y)should returnTrueif and only ify.__eq__(x)returnsTrue.The relation is transitive: for any non-null reference values
x,y, andz, ifx.__eq__(y)returnsTrueandy.__eq__(z)returnsTrue, then alsox.__eq__(z)should returnTrue.The relation is consistent: Multiple invocations of the method on the same two objects should yield the same result as long as none of the objects has been changed.
For any non-null reference value
x,x.__eq__(None)should returnFalse
Overriding __str__ and __repr__ methods
The goal of a __str__ method is to provide a string representation that is
usable and readable for a user. The goal of the __repr__ method is to be
unambiguous, see StackOverflow.
We encourage you to provide a __repr__ representation that looks like the Python
code that creates an object with the state of the object __repr__ was called on.
Consider the following example:
1class Example:
2 def __init__(
3 self, foo: str, bar: int, baz: List[str]
4 ) -> None:
5 self._foo = foo
6 self._bar = bar
7 self._baz = baz
8
9
10example = Example("abc", 42, ["xyz", "pynguin"])
The representation, i.e., the result yielded by calling __repr__ on the
example object should look like
Example(foo="abc", bar=42, baz=["xyz", "pynguin"])
which can be achieved by implementing the __repr__ method of the Example
class as follows:
1 def __repr__(self) -> str:
2 return f"Example(foo=\"{self._foo}\", bar={self._bar}, "
3 f"baz={repr(self._baz)})"
Guarding imports for type checking
Some imports in a module are only necessary for type checking but not at runtime.
We guard these imports by if typing.TYPE_CHECKING blocks.
The main reason for this is to prevent circular imports.
During type checking, these imports do not harm the type checker as it uses much more
sophisticated techniques to handle the circular imports (like a compiler does) in
contrast to the simple handling of the interpreter.
Debugging test case execution
We execute test cases in a separate thread.
To track data on the test case execution, e.g., line or branch coverage, we use thread-local storage.
Usage of threading.local may interfere with debugging tools, such as pydevd.
In such a case, disable Cython by setting the following environment variable:
PYDEVD_USE_CYTHON=NO
Code Formatting
We use ruff-format for code formatting. This replaced the previously used black formatter.
The formatting is automatically applied when running make codestyle.
Code formatting standards:
Maximum line length is 100 characters (exceptions are only permitted for imports and comments that disable linter warnings)
Imports are ordered using
isortDocstrings must conform to the Google Python Style Guide
We follow the Google Python Style Guide as much as possible
To manually format your code, you can run:
make codestyle
This will apply ruff-format to format your code and organize imports according to our standards.
In addition to formatting, we use the following tools for code quality:
rufffor static code analysismypyfor type checking
You can run all checks with:
make check
Pre-commit Hooks
Pynguin uses pre-commit to enforce code quality standards before commits are made. Pre-commit is a framework for managing and maintaining multi-language pre-commit hooks.
Pre-commit hooks are configured in the .pre-commit-config.yaml file in the project root. When you run make install, pre-commit hooks are automatically installed in your local repository.
The following pre-commit hooks are configured:
pre-commit-hooks: Various checks for code quality and formatting
check-ast: Checks Python syntaxcheck-builtin-literals: Ensures consistent use of literals for builtin typescheck-case-conflict: Checks for files with names that would conflict on a case-insensitive filesystemcheck-docstring-first: Ensures docstrings are before codecheck-json,check-toml,check-xml,check-yaml: Validates file formatscheck-merge-conflict: Checks for merge conflict markerscheck-symlinks: Checks for symlinks that don’t point to anythingdebug-statements: Checks for debugger imports and py37+breakpoint()callsdestroyed-symlinks: Detects symlinks that have been destroyedend-of-file-fixer: Ensures files end with a newlinemixed-line-ending: Ensures consistent line endingspretty-format-json: Formats JSON filestrailing-whitespace: Trims trailing whitespace
poetry hooks: Ensures Poetry configuration is valid and dependencies are up-to-date
poetry-check: Validates the structure of the pyproject.toml filepoetry-lock: Ensures poetry.lock is up-to-datepoetry-install: Ensures dependencies are installed
isort: Sorts imports according to the project’s standards
isort: Configured with black profile for compatibility
ruff: Linting and formatting
ruff: Fast Python linter with automatic fixesruff-format: Code formatter (replacement for black)
reuse-tool: Ensures license compliance
reuse: Checks for proper license headers in files
To manually run pre-commit on all files:
pre-commit run --all-files
Pre-commit hooks will also run automatically when you attempt to commit changes, preventing commits that don’t meet the project’s quality standards.
Logging
Pynguin uses Python’s standard logging module for logging. The logging system is configured in the _setup_logging function in cli.py.
Logging features:
Multiple verbosity levels (controlled via command-line options): - Default: WARNING level -
-v: INFO level --vvor higher: DEBUG levelRich console output with formatted tracebacks (can be disabled with
--no-rich)Optional file logging (enabled with
--log-fileoption)Consistent log format including timestamp, level, module name, function name, line number, and message
When adding logging to your code:
Import the logging module at the top of your file:
import logging
Create a module-level logger using:
_logger = logging.getLogger(__name__)
Use the appropriate logging level in your code:
_logger.debug("Detailed debugging information") _logger.info("General information about program execution") _logger.warning("Warning about potential issues") _logger.error("Error that doesn't prevent execution") _logger.critical("Critical error that may prevent execution")
The logging configuration can be controlled via command-line options when running Pynguin.
Adding support for new Python versions
Pynguin uses bytecode instrumentation to collect data about SUT execution. This makes Pynguin very susceptible to the changes of the CPython bytecode. Therefore, it is recommended to follow these guidelines when adding support for new Python versions:
Learn about the changes of the CPython bytecode in the documentation:
Add support for the new version of Python in the
pyproject.tomlfile and update thebytecodelibrary.Add a new module with the name of the new Python version in the
pynguin.instrumentation.versionpackage, temporarily include all symbols from the previous version and add a new case to use this module in thepynguin.instrumentation.version.__init__module.Run the tests of package
pynguin.instrumentationusing the command below and fix the issues by adding version-specific code in the module created in the previous step and removing the temporary imports of the previous version:pytest tests/instrumentationThe part that takes a fair amount of time is the
DynamicSlicerbecause for each new version of Python, the Python bytecode used in the instrumentation has to be adapted and the stack effects of each opcode have to be modified manually by checking the documentation.Run all the tests and linters of Pynguin to ensure that everything works as expected and fix any remaining bugs:
make check
Comments and DocStrings
We have no general policy regarding comments in the source code. Use them, whenever you feel they are necessary. Please do not explain what the code is doing, but why.
DocStrings are required for all public functions, methods, constructors, classes, and modules. We use the
rufflinter to check for the DocStrings. You can omit the DocString for default constructors, i.e., constructors that take no arguments, and methods that override a method from a parent class. In the former case, disableruff’s warning by adding# noqa: D107to the line of the constructor declaration; use# noqa: D102in the latter case, respectively.Please follow the Google style for your DocString formatting.
Sometimes, the interface of a class forces you to override a method, e.g., because the base class is abstract, but there is no need for a concrete implementation. In such a case put the following DocString to the overriding method to show that it is empty on purpose.