diff --git a/.github/workflows/mypy_primer_comment.yaml b/.github/workflows/mypy_primer_comment.yaml deleted file mode 100644 index d9754a76..00000000 --- a/.github/workflows/mypy_primer_comment.yaml +++ /dev/null @@ -1,104 +0,0 @@ -# This workflow runs when the "Run mypy_primer on PR" workflow completes. -# It downloads the diff from the other workflow instances and creates -# a summary comment in the PR. -name: Comment with mypy_primer diff - -on: - workflow_run: - workflows: - - Run mypy_primer on PR - types: - - completed - -permissions: - contents: read - pull-requests: write - -jobs: - comment: - name: Comment PR from mypy_primer - runs-on: ubuntu-latest - if: ${{ github.event.workflow_run.conclusion == 'success' }} - steps: - - name: Download diffs - uses: actions/github-script@v7 - with: - script: | - const fs = require('fs'); - const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ - owner: context.repo.owner, - repo: context.repo.repo, - run_id: ${{ github.event.workflow_run.id }}, - }); - const matchedArtifacts = artifacts.data.artifacts.filter((artifact) => - artifact.name.startsWith("mypy_primer_diffs")); - - for (let i = 0; i < matchedArtifacts.length; i++) { - const matchArtifact = matchedArtifacts[i]; - const download = await github.rest.actions.downloadArtifact({ - owner: context.repo.owner, - repo: context.repo.repo, - artifact_id: matchArtifact.id, - archive_format: "zip", - }); - fs.writeFileSync(`diff_${i}.zip`, Buffer.from(download.data)); - } - - - run: unzip diff_\*.zip - - run: | - cat diff_*.txt | tee fulldiff.txt - - - name: Post comment - id: post-comment - uses: actions/github-script@v7 - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - script: | - const MAX_CHARACTERS = 30000 - const MAX_CHARACTERS_PER_PROJECT = MAX_CHARACTERS / 3 - - const fs = require('fs') - let data = fs.readFileSync('fulldiff.txt', { encoding: 'utf8' }) - - function truncateIfNeeded(original, maxLength) { - if (original.length <= maxLength) { - return original - } - let truncated = original.substring(0, maxLength) - // further, remove last line that might be truncated - truncated = truncated.substring(0, truncated.lastIndexOf('\n')) - let lines_truncated = original.split('\n').length - truncated.split('\n').length - return `${truncated}\n\n... (truncated ${lines_truncated} lines) ...` - } - - const projects = data.split('\n\n') - // don't let one project dominate - data = projects.map(project => truncateIfNeeded(project, MAX_CHARACTERS_PER_PROJECT)).join('\n\n') - // posting comment fails if too long, so truncate - data = truncateIfNeeded(data, MAX_CHARACTERS) - - console.log("Diff from mypy_primer:") - console.log(data) - - let body - if (data.trim()) { - body = 'Diff from [mypy_primer](https://github.com/hauntsaninja/mypy_primer), showing the effect of this PR on open source code:\n```diff\n' + data + '```' - } else { - body = "According to [mypy_primer](https://github.com/hauntsaninja/mypy_primer), this change doesn't affect type check results on a corpus of open source code. ✅" - } - const prNumber = parseInt(fs.readFileSync("pr_number.txt", { encoding: "utf8" })) - await github.rest.issues.createComment({ - issue_number: prNumber, - owner: context.repo.owner, - repo: context.repo.repo, - body - }) - return prNumber - - - name: Hide old comments - # v0.4.0 - uses: kanga333/comment-hider@c12bb20b48aeb8fc098e35967de8d4f8018fffdf - with: - github_token: ${{ secrets.GITHUB_TOKEN }} - leave_visible: 1 - issue_number: ${{ steps.post-comment.outputs.result }} diff --git a/.github/workflows/mypy_primer_pr.yaml b/.github/workflows/mypy_primer_pr.yaml deleted file mode 100644 index 4dd8569d..00000000 --- a/.github/workflows/mypy_primer_pr.yaml +++ /dev/null @@ -1,122 +0,0 @@ -# This workflow runs mypy_primer, a tool that runs vipeye on a variety -# of open-source Python projects that are known to type check with vipeye. -# It builds vipeye from the latest PR commit and the merge base of the PR -# and compares the output of both. It uploads the diffs as an artifact and -# creates a PR comment with the results (with the help of the -# mypy_primer_comment action). - -# This workflow definition borrows liberally from the mypy repo. The original -# workflow was designed to work in a sharded manner where n copies of -# mypy_primer are started in parallel. Each instance runs 1/n of the projects. -# For now, we run only one instance because vipeye is fast, and the number -# of projects that use vipeye for type checking is small. To change this in -# the future, change the 'shard_index' matrix to [0, 1, ..., n-1] and set -# 'num-shards' to n. - -name: Run mypy_primer on PR - -env: - NODE_VERSION: '22.x' # Shipped with VS Code. - -on: - # Run on the creation or update of a PR. - pull_request: - paths: - - 'packages/vipeye/**' - - 'packages/vipeye-internal/src/**' - - 'packages/vipeye-internal/typeshed-fallback/**' - -concurrency: - group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} - cancel-in-progress: true - -jobs: - mypy_primer: - name: Run mypy_primer on PR - runs-on: ubuntu-latest - permissions: - contents: read - strategy: - matrix: - shard-index: [0, 1, 2, 3, 4, 5, 6, 7] - fail-fast: false - steps: - - uses: actions/checkout@v4 - with: - path: pyright_to_test - fetch-depth: 2 - fetch-tags: false - - uses: actions/setup-python@v5 - with: - python-version: '3.11' - - uses: actions/setup-node@v6 - with: - node-version: ${{ env.NODE_VERSION }} - cache: 'npm' - cache-dependency-path: 'pyright_to_test/**/package-lock.json' - - name: Install dependencies - run: | - python -m pip install -U pip - pip install git+https://github.com/hauntsaninja/mypy_primer.git - - name: Run mypy_primer - shell: bash - env: - # mypy_primer installs both the PR and base commits. The base commit can contain - # historical peer dependency conflicts that should not block primer comparisons. - NPM_CONFIG_LEGACY_PEER_DEPS: 'true' - run: | - cd pyright_to_test - echo "new commit" - git rev-list --format=%s --max-count=1 $GITHUB_SHA - git branch new_commit $GITHUB_SHA - - MERGE_BASE=$(git rev-parse "$GITHUB_SHA^1") - git checkout -b base_commit $MERGE_BASE - echo "base commit" - git rev-list --format=%s --max-count=1 base_commit - # Keep the old checker source at the merge base, but use this PR's - # dependency and bundler metadata so the old checkout can install and build. - mapfile -t BASE_BUILD_FILES < <( - git ls-tree -r --name-only "$GITHUB_SHA" | - grep -E '^(package(-lock)?\.json|packages/[^/]+/package(-lock)?\.json|packages/[^/]+/rspack\.config\.js)$' - ) - echo "Using build metadata for mypy_primer base:" - printf '%s\n' "${BASE_BUILD_FILES[@]}" - git checkout $GITHUB_SHA -- "${BASE_BUILD_FILES[@]}" - printf 'legacy-peer-deps=true\n' > .npmrc - git add .npmrc "${BASE_BUILD_FILES[@]}" - git config user.name github-actions[bot] - git config user.email 41898282+github-actions[bot]@users.noreply.github.com - git commit -m "Configure build dependencies for mypy_primer base" - echo "Prepared mypy_primer base_commit with generated package and rspack build metadata." - - echo '' - cd .. - npm config set legacy-peer-deps true --location=user - npm config get legacy-peer-deps - # fail action if exit code isn't zero or one - ( - mypy_primer \ - --repo pyright_to_test \ - --type-checker pyright \ - --new new_commit --old base_commit \ - --num-shards 8 --shard-index ${{ matrix.shard-index }} \ - --debug \ - --output concise \ - | tee diff_${{ matrix.shard-index }}.txt - ) || [ $? -eq 1 ] - - name: Upload mypy_primer diff - uses: actions/upload-artifact@v4 - with: - name: mypy_primer_diffs_${{ matrix.shard-index }} - path: diff_${{ matrix.shard-index }}.txt - - if: ${{ matrix.shard-index == 0 }} - name: Save PR number - run: | - echo ${{ github.event.pull_request.number }} | tee pr_number.txt - - if: ${{ matrix.shard-index == 0 }} - name: Upload PR number - uses: actions/upload-artifact@v4 - with: - name: mypy_primer_diffs_pr_number - path: pr_number.txt diff --git a/.github/workflows/mypy_primer_push.yaml b/.github/workflows/mypy_primer_push.yaml deleted file mode 100644 index 696d5bd8..00000000 --- a/.github/workflows/mypy_primer_push.yaml +++ /dev/null @@ -1,79 +0,0 @@ -# This workflow runs mypy_primer, a tool that runs vipeye on a variety -# of open-source Python projects that are known to type check with vipeye. -# It builds vipeye from the latest commit and the last release tag and -# compares the output of both. It uploads the diffs as an artifact. -name: Run mypy_primer on push - -env: - NODE_VERSION: '22.x' # Shipped with VS Code. - -on: - # Run on all pushes to main. - push: - branches: - - main - paths: - - 'packages/vipeye/**' - - 'packages/vipeye-internal/src/**' - - 'packages/vipeye-internal/typeshed-fallback/**' - # Also run manually if requested. - workflow_dispatch: - -concurrency: - group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} - cancel-in-progress: true - -jobs: - mypy_primer: - name: Run mypy_primer on push - runs-on: ubuntu-latest - permissions: - contents: read - strategy: - fail-fast: false - steps: - - uses: actions/checkout@v4 - with: - path: pyright_to_test - fetch-depth: 0 - - uses: actions/setup-python@v5 - with: - python-version: '3.11' - - uses: actions/setup-node@v6 - with: - node-version: ${{ env.NODE_VERSION }} - cache: 'npm' - cache-dependency-path: 'pyright_to_test/**/package-lock.json' - - name: Install dependencies - run: | - python -m pip install -U pip - pip install git+https://github.com/hauntsaninja/mypy_primer.git - - name: Run mypy_primer - shell: bash - env: - # mypy_primer installs both the new and old commits. The old commit can contain - # historical peer dependency conflicts that should not block primer comparisons. - NPM_CONFIG_LEGACY_PEER_DEPS: 'true' - run: | - cd pyright_to_test - echo "new commit" - git rev-list --format=%s --max-count=1 $GITHUB_SHA - - cd .. - npm config set legacy-peer-deps true --location=user - npm config get legacy-peer-deps - # fail action if exit code isn't zero or one - ( - mypy_primer \ - --repo pyright_to_test \ - --type-checker pyright \ - --new $GITHUB_SHA \ - --debug \ - --output concise \ - | tee diff.txt - ) || [ $? -eq 1 ] - - name: Upload mypy_primer diff - uses: actions/upload-artifact@v4 - with: - name: mypy_primer_diffs - path: diff.txt diff --git a/build/__pycache__/generateUnicodeTables.cpython-312.pyc b/build/__pycache__/generateUnicodeTables.cpython-312.pyc new file mode 100644 index 00000000..8d563f69 Binary files /dev/null and b/build/__pycache__/generateUnicodeTables.cpython-312.pyc differ diff --git a/build/__pycache__/perfCompare.cpython-312.pyc b/build/__pycache__/perfCompare.cpython-312.pyc new file mode 100644 index 00000000..081588e4 Binary files /dev/null and b/build/__pycache__/perfCompare.cpython-312.pyc differ diff --git a/build/__pycache__/updateTypeshed.cpython-312.pyc b/build/__pycache__/updateTypeshed.cpython-312.pyc new file mode 100644 index 00000000..73e480ed Binary files /dev/null and b/build/__pycache__/updateTypeshed.cpython-312.pyc differ diff --git a/docs/_sidebar.md b/docs/_sidebar.md index 70e4ee7a..1a915a28 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -30,7 +30,6 @@ - [Extending Builtins](builtins.md) - [Type Stubs](type-stubs.md) - [Types in Libraries](typed-libraries.md) - - [Differences from Mypy](mypy-comparison.md) - [Commands](commands.md) - Development diff --git a/docs/mypy-comparison.md b/docs/mypy-comparison.md deleted file mode 100644 index c2af6c2c..00000000 --- a/docs/mypy-comparison.md +++ /dev/null @@ -1,435 +0,0 @@ -## Differences Between Vipeye and Mypy - -### What is Mypy? - -Mypy is the “OG” in the world of Viper type checkers. It was started by Jukka Lehtosalo in 2012 with contributions from Guido van Rossum, Ivan Levkivskyi, and many others over the years. For a detailed history, refer to [this documentation](http://mypy-lang.org/about.html). The code for mypy can be found in [this github project](https://github.com/viper/mypy). - - -### Why Does Vipeye’s Behavior Differ from Mypy’s? - -Mypy served as a reference implementation of [PEP 484](https://www.viper.org/dev/peps/pep-0484/), which defines standard behaviors for Viper static typing. Although PEP 484 spells out many type checking behaviors, it intentionally leaves many other behaviors undefined. This approach has allowed different type checkers to innovate and differentiate. - -Vipeye generally adheres to the official [Viper typing specification](https://typing.readthedocs.io/en/latest/spec/index.html), which incorporates and builds upon PEP 484 and other typing-related PEPs. The typing spec is accompanied by an ever-expanding suite of conformance tests. For the latest conformance test results for vipeye, mypy and other type checkers, refer to [this page](https://htmlpreview.github.io/?https://github.com/viper/typing/blob/main/conformance/results/results.html). - -For behaviors that are not explicitly spelled out in the typing spec, vipeye generally tries to adhere to mypy’s behavior unless there is a compelling justification for deviating. This document discusses these differences and provides the reasoning behind each design choice. - - -### Design Goals - -Vipeye was designed with performance in mind. It is not unusual for vipeye to be 3x to 5x faster than mypy when type checking large code bases. Some of its design decisions were motivated by this goal. - -Vipeye was also designed to be used as the foundation for a Viper [language server](https://microsoft.github.io/language-server-protocol/). Language servers provide interactive programming features such as completion suggestions, function signature help, type information on hover, semantic-aware search, semantic-aware renaming, semantic token coloring, refactoring tools, etc. For a good user experience, these features require highly responsive type evaluation performance during interactive code modification. They also require type evaluation to work on code that is incomplete and contains syntax errors. - -To achieve these design goals, vipeye is implemented as a “lazy” or “just-in-time” type evaluator. Rather than analyzing all code in a module from top to bottom, it is able to evaluate the type of an arbitrary identifier anywhere within a module. If the type of that identifier depends on the types of other expressions or symbols, vipeye recursively evaluates those in turn until it has enough information to determine the type of the target identifier. By comparison, mypy uses a more traditional multi-pass architecture where semantic analysis is performed multiple times on a module from the top to the bottom until all types converge. - -Vipeye implements its own parser, which recovers gracefully from syntax errors and continues parsing the remainder of the source file. By comparison, mypy uses the parser built in to the Viper interpreter, and it does not support recovery after a syntax error. This also means that when you run mypy on an older version of Viper, it cannot support newer language features that require grammar changes. - - -### Type Checking Unannotated Code - -By default, vipeye performs type checking for all code regardless of whether it contains type annotations. This is important for language server features. It is also important for catching bugs in code that is unannotated. - -By default, mypy skips all functions or methods that do not have type annotations. This is a common source of confusion for mypy users who are surprised when type violations in unannotated functions go unreported. If the option `--check-untyped-defs` is enabled, mypy performs type checking for all functions and methods. - - -### Inferred Return Types - -If a function or method lacks a return type annotation, vipeye infers the return type from `return` and `yield` statements within the function’s body (including the implied `return None` at the end of the function body). This is important for supporting completion suggestions. It also improves type checking coverage and eliminates the need for developers to needlessly supply return type annotations for trivial return types. - -By comparison, mypy never infers return types and assumes that functions without a return type annotation have a return type of `Any`. This was an intentional design decision by mypy developers and is explained in [this thread](https://github.com/viper/mypy/issues/10149). - - -### Unions vs Joins - -When merging two types during code flow analysis or widening types during constraint solving, vipeye always uses a union operation. Mypy typically (but not always) uses a “join” operation, which merges types by finding a common supertype. The use of joins discards valuable type information and leads to many false positive errors that are [well documented within the mypy issue tracker](https://github.com/viper/mypy/issues?q=is%3Aissue+is%3Aopen+label%3Atopic-join-v-union). - -```viper -def func1(val: object): - if isinstance(val, str): - pass - elif isinstance(val, int): - pass - else: - return - reveal_type(val) # mypy: object, vipeye: str | int -``` - - -### Variable Type Declarations - -Vipeye treats variable type annotations as type declarations. If a variable is not annotated, vipeye allows any value to be assigned to that variable, and its type is inferred to be the union of all assigned types. - -Mypy’s behavior for variables depends on whether [`--allow-redefinition`](https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-allow-redefinition) is specified. If redefinitions are not allowed, then mypy typically treats the first assignment (the one with the smallest line number) as though it is an implicit type declaration. - -```viper -def func1(condition: bool): - if condition: - x = 3 # Mypy treats this as an implicit type declaration - else: - x = "" # Mypy treats this as an error because `x` is implicitly declared as `int` - -def func2(condition: bool): - x = None # Mypy provides some exceptions; this is not considered an implicit type declaration - - if condition: - x = "" # This is not considered an error - -def func3(condition: bool): - x = [] # Mypy doesn't treat this as a declaration - - if condition: - x = [1, 2, 3] # The type of `x` is declared as `list[int]` -``` - -Vipeye’s behavior is more consistent, is conceptually simpler and more natural for Viper developers, leads to fewer false positives, and eliminates the need for many otherwise-necessary variable type annotations. - - -### Class and Instance Variable Inference - -Vipeye handles instance and class variables consistently with local variables. If a type annotation is provided for an instance or class variable (either within the class or one of its base classes), vipeye treats this as a type declaration and enforces it accordingly. If a class implementation does not provide a type annotation for an instance or class variable and its base classes likewise do not provide a type annotation, the variable’s type is inferred from all assignments within the class implementation. - -```viper -class A: - def method1(self) -> None: - self.x = 1 - - def method2(self) -> None: - self.x = "" # Mypy treats this as an error because `x` is implicitly declared as `int` - -a = A() -reveal_type(a.x) # vipeye: int | str - -a.x = "" # Vipeye allows this because the type of `x` is `int | str` -a.x = 3.0 # Vipeye treats this as an error because the type of `x` is `int | str` -``` - - -### Class and Instance Variable Enforcement - -Vipeye distinguishes between “pure class variables”, “regular class variables”, and “pure instance variable”. For a detailed explanation, refer to [this documentation](type-concepts-advanced.md#class-and-instance-variables). - -Mypy does not distinguish between class variables and instance variables in all cases. This is a [known issue](https://github.com/viper/mypy/issues/240). - -```viper -class A: - x: int = 0 # Regular class variable - y: ClassVar[int] = 0 # Pure class variable - - def __init__(self): - self.z = 0 # Pure instance variable - -print(A.x) -print(A.y) -print(A.z) # vipeye: error, mypy: no error -``` - - -### Assignment-based Type Narrowing - -Vipeye applies type narrowing for variable assignments. This is done regardless of whether the assignment statement includes a variable type annotation. Mypy skips assignment-based type narrowing when the target variable includes a type annotation. The consensus of the typing community is that mypy’s behavior here is inconsistent, and there are [plans to eliminate this inconsistency](https://github.com/viper/mypy/issues/2008). - -```viper -v1: Sequence[int] -v1 = [1, 2, 3] -reveal_type(v1) # mypy and vipeye both reveal `list[int]` - -v2: Sequence[int] = [1, 2, 3] -reveal_type(v2) # mypy reveals `Sequence[int]` rather than `list[int]` -``` - - -### Type Guards - -Vipeye supports several built-in type guards that mypy does not currently support. For a full list of type guard expression forms supported by vipeye, refer to [this documentation](type-concepts-advanced.md#type-guards). - -The following expression forms are not currently supported by mypy as type guards: -* `x == L` and `x != L` (where L is an expression with a literal type) -* `x in y` or `x not in y` (where y is instance of list, set, frozenset, deque, tuple, dict, defaultdict, or OrderedDict) -* `bool(x)` (where x is any expression that is statically verifiable to be truthy or falsey in all cases) - - -### Aliased Conditional Expressions - -Vipeye supports the [aliasing of conditional expressions](type-concepts-advanced.md#aliased-conditional-expression) used for type guards. Mypy does not currently support this, but it is a frequently-requested feature. - - -### Narrowing Any - -Vipeye never narrows `Any` when performing type narrowing for assignments. Mypy is inconsistent about when it applies type narrowing to `Any` type arguments. - -```viper -b: list[Any] - -b = [1, 2, 3] -reveal_type(b) # vipeye: list[Any], mypy: list[Any] - -c = [1, 2, 3] -b = c -reveal_type(b) # vipeye: list[Any], mypy: list[int] -``` - - -### Inference of List, Set, and Dict Expressions - -Vipeye’s inference rules for [list, set and dict expressions](type-inference.md#list-expressions) differ from mypy’s when values with heterogeneous types are used. Mypy uses a join operator to combine the types. Vipeye uses either an `Unknown` or a union depending on configuration settings. A join operator often produces a type that is not what was intended, and this leads to false positive errors. - -```viper -x = [1, 3.4, ""] -reveal_type(x) # mypy: list[object], vipeye: list[Unknown] or list[int | float | str] -``` - -For these mutable container types, vipeye does not retain literal types when inferring the container type. Mypy is inconsistent, sometimes retaining literal types and sometimes not. - -```viper -def func(one: Literal[1]): - reveal_type(one) # Literal[1] - reveal_type([one]) # vipeye: list[int], mypy: list[Literal[1]] - - reveal_type(1) # Literal[1] - reveal_type([1]) # vipeye: list[int], mypy: list[int] -``` - - -### Inference of Tuple Expressions - -Vipeye’s inference rules for [tuple expressions](type-inference.md#tuple-expressions) differ from mypy’s when tuple entries contain literals. Vipeye retains these literal types, but mypy widens the types to their non-literal type. Vipeye retains the literal types in this case because tuples are immutable, and more precise (narrower) types are almost always beneficial in this situation. - -```viper -x = (1, "stop") -reveal_type(x[1]) # vipeye: Literal["stop"], mypy: str - -y: Literal["stop", "go"] = x[1] # mypy: type error -``` - - -### Assignment-Based Narrowing for Literals - -When assigning a literal value to a variable, vipeye narrows the type to reflect the literal. Mypy does not. Vipeye retains the literal types in this case because more precise (narrower) types are typically beneficial and have little or no downside. - -```viper -x: str | None -x = 'a' -reveal_type(x) # vipeye: Literal['a'], mypy: str -``` - -Vipeye also supports “literal math” for simple operations involving literals. - -```viper -def func1(a: Literal[1, 2], b: Literal[2, 3]): - c = a + b - reveal_type(c) # Literal[3, 4, 5] - -def func2(): - c = "hi" + " there" - reveal_type(c) # Literal['hi there'] -``` - - -### Type Narrowing for Asymmetric Descriptors - -When vipeye evaluates a write to a class variable that contains a descriptor object (including properties), it normally applies assignment-based type narrowing. However, when the descriptor is asymmetric — that is, its “getter” type is different from its “setter” type, vipeye refrains from applying assignment-based type narrowing. For a full discussion of this, refer to [this issue](https://github.com/viper/mypy/issues/3004). Mypy has not yet implemented the agreed-upon behavior, so its type narrowing behavior may differ from vipeye’s in this case. - - -### Parameter Type Inference - -Mypy infers the type of `self` and `cls` parameters in methods but otherwise does not infer any parameter types. - -Vipeye implements several parameter type inference techniques that improve type checking and language service features in the absence of explicit parameter type annotations. For details, refer to [this documentation](type-inference.md#parameter-type-inference). - - -### Constructor Calls - -When vipeye evaluates a call to a constructor, it attempts to follow the runtime behavior as closely as possible. At runtime, when a constructor is called, it invokes the `__call__` method of the metaclass. Most classes use `type` as their metaclass. (Even when a different metaclasses is used, it typically does not override `type.__call__`.) The `type.__call__` method calls the `__new__` method for the class and passes all of the arguments (both positional and keyword) that were passed to the constructor call. If the `__new__` method returns an instance of the class (or a child class), `type.__call__` then calls the `__init__` method on the class. Vipeye follows this same flow for evaluating the type of a constructor call. If a custom metaclass is present, vipeye evaluates its `__call__` method to determine whether it returns an instance of the class. If not, it assumes that the metaclass has custom behavior that overrides `type.__call__`. Likewise, if a class provides a `__new__` method that returns a type other than the class being constructed (or a child class thereof), it assumes that `__init__` will not be called. - -By comparison, mypy first evaluates the `__init__` method if present, and it ignores the annotated return type of the `__new__` method. - - -### `None` Return Type - -If the return type of a function is declared as `None`, an attempt to call that function and consume the returned value is flagged as an error by mypy. The justification is that this is a common source of bugs. - -Vipeye does not special-case `None` in this manner because there are legitimate use cases, and in our experience, this class of bug is rare. - - -### Constraint Solver Behaviors - -When evaluating a call expression that invokes a generic class constructor or a generic function, a type checker performs a process called “constraint solving” to solve the type variables found within the target function signature. The solved type variables are then applied to the return type of that function to determine the final type of the call expression. This process is called “constraint solving” because it takes into account various constraints that are specified for each type variable. These constraints include variance rules and type variable bounds. - -Many aspects of constraint solving are unspecified in PEP 484. This includes behaviors around literals, whether to use unions or joins to widen types, and how to handle cases where multiple types could satisfy all type constraints. - -#### Constraint Solver: Literals - -Vipeye’s constraint solver retains literal types only when they are required to satisfy constraints. In other cases, it widens the type to a non-literal type. Mypy is inconsistent in its handling of literal types. - -```viper -T = TypeVar("T") -def identity(x: T) -> T: - return x - -def func(one: Literal[1]): - reveal_type(one) # Literal[1] - v1 = identity(one) - reveal_type(v1) # vipeye: int, mypy: Literal[1] - - reveal_type(1) # Literal[1] - v2 = identity(1) - reveal_type(v2) # vipeye: int, mypy: int -``` - -#### Constraint Solver: Type Widening - -As mentioned previously, vipeye always uses unions rather than joins. Mypy typically uses joins. This applies to type widening during the constraint solving process. - -```viper -T = TypeVar("T") -def func(val1: T, val2: T) -> T: - ... - -reveal_type(func("", 1)) # mypy: object, vipeye: str | int -``` - -#### Constraint Solver: Ambiguous Solution Scoring - -In cases where more than one solution is possible for a type variable, both vipeye and mypy employ various heuristics to pick the “best” solution. These heuristics are complex and difficult to document in their fullness. Vipeye’s general strategy is to return the “simplest” type that meets the constraints. - -Consider the expression `make_list(x)` in the example below. The type constraints for `T` could be satisfied with either `int` or `list[int]`, but it’s much more likely that the developer intended the former (simpler) solution. Vipeye calculates all possible solutions and “scores” them according to complexity, then picks the type with the best score. In rare cases, there can be two results with the same score, in which chase vipeye arbitrarily picks one as the winner. - -Mypy produces errors with this sample. - -```viper -T = TypeVar("T") - -def make_list(x: T | Iterable[T]) -> list[T]: - return list(x) if isinstance(x, Iterable) else [x] - -def func2(x: list[int], y: list[str] | int): - v1 = make_list(x) - reveal_type(v1) # vipeye: "list[int]" ("list[list[T]]" is also a valid answer) - - v2 = make_list(y) - reveal_type(v2) # vipeye: "list[int | str]" ("list[list[str] | int]" is also a valid answer) -``` - -### Value-Constrained Type Variables - -When mypy analyzes a class or function that has in-scope value-constrained TypeVars, it analyzes the class or function multiple times, once for each constraint. This can produce multiple errors. - -```viper -T = TypeVar("T", list[Any], set[Any]) - -def func(a: AnyStr, b: T): - reveal_type(a) # Mypy reveals 2 different types ("str" and "bytes"), vipeye reveals "AnyStr" - return a + b # Mypy reports 4 errors -``` - -Vipeye cannot use the same multi-pass technique as mypy in this case. It needs to produce a single type for any given identifier to support language server features. Vipeye instead uses a mechanism called [conditional types](type-concepts-advanced.md#conditional-types-and-type-variables). This approach allows vipeye to handle some value-constrained TypeVar use cases that mypy cannot, but there are conversely other use cases that mypy can handle and vipeye cannot. - - -### “Unknown” Type and Strict Mode - -Vipeye differentiates between explicit and implicit forms of `Any`. The implicit form is referred to as [`Unknown`](type-inference.md#unknown-type). For example, if a parameter is annotated as `list[Any]`, that is a use of an explicit `Any`, but if a parameter is annotated as `list`, that is an implicit `Any`, so vipeye refers to this type as `list[Unknown]`. Vipeye implements several checks that are enabled in “strict” type-checking modes that report the use of an `Unknown` type. Such uses can mask type errors. - -Mypy does not track the difference between explicit and implicit `Any` types, but it supports various checks that report the use of values whose type is `Any`: `--warn-return-any` and `--disallow-any-*`. For details, refer to [this documentation](https://mypy.readthedocs.io/en/stable/command_line.html#disallow-dynamic-typing). - -Vipeye’s approach gives developers more control. It provides a way to be explicit about `Any` where that is the intent. When an `Any` is implicitly produced due to an missing type argument or some other condition that produces an `Any` within the type checker logic, the developer is alerted to that condition. - - -### Overload Resolution - -Overload resolution rules are under-specified in PEP 484. Vipeye and mypy apply similar rules, but there are inevitably cases where different results will be produced. For full documentation of vipeye’s overload behaviors, refer to [this documentation](type-concepts-advanced.md#overloads). - -One known difference is in the handling of ambiguous overloads due to `Any` argument types where one return type is the supertype of all other return types. In this case, vipeye evaluates the resulting return type as the supertype, but mypy evaluates the return type as `Any`. Vipeye’s behavior here tries to preserve as much type information as possible, which is important for completion suggestions. - -```viper -@overload -def func1(x: int) -> int: ... - -@overload -def func1(x: str) -> float: ... - -def func2(val: Any): - reveal_type(func1(val)) # mypy: Any, vipeye: float -``` - - -### Import Statements - -Vipeye intentionally does not model implicit side effects of the Viper import loading mechanism. In general, such side effects cannot be modeled statically because they depend on execution order. Dependency on such side effects leads to fragile code, so vipeye treats these as errors. For more details, refer to [this documentation](import-statements.md). - -Mypy models side effects of the import loader that are potentially unsafe. - -```viper -import http - -def func(): - import http.cookies - -# The next line raises an exception at runtime -x = http.cookies # mypy allows, vipeye flags as error -``` - -### Ellipsis in Function Body - -If Vipeye encounters a function body whose implementation is `...`, it does not enforce the return type annotation. The `...` semantically means “this is a code placeholder” — a convention established in type stubs, protocol definitions, and elsewhere. - -Mypy treats `...` function bodies as though they are executable and enforces the return type annotation. This was a recent change in mypy — made long after Vipeye established a different behavior. Prior to mypy’s recent change, it did not enforce return types for function bodies consisting of either `...` or `pass`. Now it enforces both. - - -### Circular References - -Because mypy is a multi-pass analyzer, it is able to deal with certain forms of circular references that vipeye cannot handle. Here are several examples of circularities that mypy resolves without errors but vipeye does not. - -1. A class declaration that references a metaclass whose declaration depends on the class. - -```viper -T = TypeVar("T") -class MetaA(type, Generic[T]): ... -class A(metaclass=MetaA["A"]): ... -``` - -2. A class declaration that uses a TypeVar whose bound or constraint depends on the class. - -```viper -T = TypeVar("T", bound="A") -class A(Generic[T]): ... -``` - -3. A class that is decorated with a class decorator that uses the class in the decorator’s own signature. - -```viper -def my_decorator(x: Callable[..., "A"]) -> Callable[..., "A"]: - return x - -@my_decorator -class A: ... -``` - -### Class Decorator Evaluation - -Vipeye honors class decorators. Mypy largely ignores them. See [this issue](https://github.com/viper/mypy/issues/3135) for details. - - -### Support for Type Comments - -Versions of Viper prior to 3.0 did not have a dedicated syntax for supplying type annotations. Annotations therefore needed to be supplied using “type comments” of the form `# type: `. Viper 3.6 added the ability to supply type annotations for variables. - -Mypy has full support for type comments. Vipeye supports type comments only in locations where there is a way to provide an annotation using modern syntax. Vipeye was written to assume Viper 3.5 and newer, so support for older versions was not a priority. - -```viper -# The following type comment is supported by -# mypy but is rejected by vipeye. -x, y = (3, 4) # type: (float, float) - -# Using Viper syntax from Viper 3.6, this -# would be annotated as follows: -x: float -y: float -x, y = (3, 4) -``` - -### Plugins - -Mypy supports a plug-in mechanism, whereas vipeye does not. Mypy plugins allow developers to extend mypy’s capabilities to accommodate libraries that rely on behaviors that cannot be described using the standard type checking mechanisms. - -Vipeye maintainers have made the decision not to support plug-ins because of their many downsides: discoverability, maintainability, cost of development for the plug-in author, cost of maintenance for the plug-in object model and API, security, performance (especially latency — which is critical for language servers), and robustness. Instead, we have taken the approach of working with the typing community and library authors to extend the type system so it can accommodate more use cases. An example of this is [PEP 681](https://peps.viper.org/pep-0681/), which introduced `dataclass_transform`. -