diff --git a/.github/dependabot.yml b/.github/dependabot.yml index e2b415d22c4a..5cb33a8cf813 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -5,6 +5,9 @@ updates: groups: github-actions: patterns: ['*'] + # gh-aw owns this runtime and its generated script calls; upgrade them together. + ignore: + - dependency-name: 'github/gh-aw-actions' schedule: interval: 'weekly' cooldown: diff --git a/.github/workflows/mypy-primer-analysis.lock.yml b/.github/workflows/mypy-primer-analysis.lock.yml index bcb996c47cda..bcf02a49d9bd 100644 --- a/.github/workflows/mypy-primer-analysis.lock.yml +++ b/.github/workflows/mypy-primer-analysis.lock.yml @@ -1,5 +1,5 @@ -# gh-aw-metadata: {"schema_version":"v4","frontmatter_hash":"b0ff5796747e09647167a3eb460c3b61b4f5b2258f027a48660e607709e39943","body_hash":"aa52c647593a2852ca78df1f3ec87d866b86ed6aa045e76f1759e10954c9a61b","compiler_version":"v0.86.2","strict":true,"agent_id":"copilot","engine_versions":{"copilot":"1.0.79"}} -# gh-aw-manifest: {"version":1,"secrets":["GH_AW_GITHUB_MCP_SERVER_TOKEN","GH_AW_GITHUB_TOKEN","GITHUB_TOKEN"],"actions":[{"repo":"actions/cache/restore","sha":"55cc8345863c7cc4c66a329aec7e433d2d1c52a9","version":"v6.1.0"},{"repo":"actions/cache/save","sha":"55cc8345863c7cc4c66a329aec7e433d2d1c52a9","version":"v6.1.0"},{"repo":"actions/checkout","sha":"3d3c42e5aac5ba805825da76410c181273ba90b1","version":"v7.0.1"},{"repo":"actions/download-artifact","sha":"3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c","version":"v8.0.1"},{"repo":"actions/download-artifact","sha":"634f93cb2916e3fdff6788551b99b062d0335ce0","version":"634f93cb2916e3fdff6788551b99b062d0335ce0"},{"repo":"actions/github-script","sha":"3a2844b7e9c422d3c10d287c895573f7108da1b3","version":"v9.0.0"},{"repo":"actions/setup-node","sha":"820762786026740c76f36085b0efc47a31fe5020","version":"v7.0.0"},{"repo":"actions/upload-artifact","sha":"043fb46d1a93c77aae656e7c1c64a875d1fc6a0a","version":"v7.0.1"},{"repo":"actions/upload-artifact","sha":"ea165f8d65b6e75b540449e92b4886f43607fa02","version":"ea165f8d65b6e75b540449e92b4886f43607fa02"},{"repo":"github/gh-aw-actions/setup","sha":"6aab9e5b5c91c615506061f09bedd81a23babe3c","version":"v0.86.2"}],"containers":[{"image":"ghcr.io/github/gh-aw-firewall/agent:0.27.44","digest":"sha256:0d727725c737b58c7bdf51f640cffb928385ec46517e0917c7f1a02f1bada8b4","pinned_image":"ghcr.io/github/gh-aw-firewall/agent:0.27.44@sha256:0d727725c737b58c7bdf51f640cffb928385ec46517e0917c7f1a02f1bada8b4"},{"image":"ghcr.io/github/gh-aw-firewall/api-proxy:0.27.44","digest":"sha256:b50fbadba138f6e9aba94aca09711335c489bb3b15861220cb66f6092e042dc7","pinned_image":"ghcr.io/github/gh-aw-firewall/api-proxy:0.27.44@sha256:b50fbadba138f6e9aba94aca09711335c489bb3b15861220cb66f6092e042dc7"},{"image":"ghcr.io/github/gh-aw-firewall/squid:0.27.44","digest":"sha256:83e48bbe12c634be8c228a576832fe45f66c529ac3659db92bddbcf2eeb6d627","pinned_image":"ghcr.io/github/gh-aw-firewall/squid:0.27.44@sha256:83e48bbe12c634be8c228a576832fe45f66c529ac3659db92bddbcf2eeb6d627"},{"image":"ghcr.io/github/gh-aw-mcpg:v0.4.9","digest":"sha256:e5a1569aeaf41820fa7bdee3e94468cae448133cdbf00119ad24f5b74db1ab9f","pinned_image":"ghcr.io/github/gh-aw-mcpg:v0.4.9@sha256:e5a1569aeaf41820fa7bdee3e94468cae448133cdbf00119ad24f5b74db1ab9f"},{"image":"ghcr.io/github/gh-aw-node","digest":"sha256:0d9f1fb5fd6610c0ac1f5194a38e45a8a1e81f8a390d5142d8e4e6f26a4b3196","pinned_image":"ghcr.io/github/gh-aw-node@sha256:0d9f1fb5fd6610c0ac1f5194a38e45a8a1e81f8a390d5142d8e4e6f26a4b3196"},{"image":"ghcr.io/github/github-mcp-server:v1.9.0","digest":"sha256:881b53d6f75f69bdbc1b5b10fc2f1361717c19054143b3a8529fb5c32061a50e","pinned_image":"ghcr.io/github/github-mcp-server:v1.9.0@sha256:881b53d6f75f69bdbc1b5b10fc2f1361717c19054143b3a8529fb5c32061a50e"}]} +# gh-aw-metadata: {"schema_version":"v4","frontmatter_hash":"59d7c3321e8c3e71d1ed36ef08fa18098d0b9d2abcd71c00cc98535367afd047","body_hash":"aa52c647593a2852ca78df1f3ec87d866b86ed6aa045e76f1759e10954c9a61b","compiler_version":"v0.86.2","strict":true,"agent_id":"copilot","engine_versions":{"copilot":"1.0.79"}} +# gh-aw-manifest: {"version":1,"secrets":["GH_AW_GITHUB_MCP_SERVER_TOKEN","GH_AW_GITHUB_TOKEN","GITHUB_TOKEN"],"actions":[{"repo":"actions/cache/restore","sha":"55cc8345863c7cc4c66a329aec7e433d2d1c52a9","version":"v6.1.0"},{"repo":"actions/cache/save","sha":"55cc8345863c7cc4c66a329aec7e433d2d1c52a9","version":"v6.1.0"},{"repo":"actions/checkout","sha":"3d3c42e5aac5ba805825da76410c181273ba90b1","version":"v7.0.1"},{"repo":"actions/download-artifact","sha":"3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c","version":"v8.0.1"},{"repo":"actions/github-script","sha":"3a2844b7e9c422d3c10d287c895573f7108da1b3","version":"v9.0.0"},{"repo":"actions/setup-node","sha":"820762786026740c76f36085b0efc47a31fe5020","version":"v7.0.0"},{"repo":"actions/upload-artifact","sha":"043fb46d1a93c77aae656e7c1c64a875d1fc6a0a","version":"v7.0.1"},{"repo":"github/gh-aw-actions/setup","sha":"6aab9e5b5c91c615506061f09bedd81a23babe3c","version":"v0.86.2"}],"containers":[{"image":"ghcr.io/github/gh-aw-firewall/agent:0.27.44","digest":"sha256:0d727725c737b58c7bdf51f640cffb928385ec46517e0917c7f1a02f1bada8b4","pinned_image":"ghcr.io/github/gh-aw-firewall/agent:0.27.44@sha256:0d727725c737b58c7bdf51f640cffb928385ec46517e0917c7f1a02f1bada8b4"},{"image":"ghcr.io/github/gh-aw-firewall/api-proxy:0.27.44","digest":"sha256:b50fbadba138f6e9aba94aca09711335c489bb3b15861220cb66f6092e042dc7","pinned_image":"ghcr.io/github/gh-aw-firewall/api-proxy:0.27.44@sha256:b50fbadba138f6e9aba94aca09711335c489bb3b15861220cb66f6092e042dc7"},{"image":"ghcr.io/github/gh-aw-firewall/squid:0.27.44","digest":"sha256:83e48bbe12c634be8c228a576832fe45f66c529ac3659db92bddbcf2eeb6d627","pinned_image":"ghcr.io/github/gh-aw-firewall/squid:0.27.44@sha256:83e48bbe12c634be8c228a576832fe45f66c529ac3659db92bddbcf2eeb6d627"},{"image":"ghcr.io/github/gh-aw-mcpg:v0.4.9","digest":"sha256:e5a1569aeaf41820fa7bdee3e94468cae448133cdbf00119ad24f5b74db1ab9f","pinned_image":"ghcr.io/github/gh-aw-mcpg:v0.4.9@sha256:e5a1569aeaf41820fa7bdee3e94468cae448133cdbf00119ad24f5b74db1ab9f"},{"image":"ghcr.io/github/gh-aw-node","digest":"sha256:0d9f1fb5fd6610c0ac1f5194a38e45a8a1e81f8a390d5142d8e4e6f26a4b3196","pinned_image":"ghcr.io/github/gh-aw-node@sha256:0d9f1fb5fd6610c0ac1f5194a38e45a8a1e81f8a390d5142d8e4e6f26a4b3196"},{"image":"ghcr.io/github/github-mcp-server:v1.9.0","digest":"sha256:881b53d6f75f69bdbc1b5b10fc2f1361717c19054143b3a8529fb5c32061a50e","pinned_image":"ghcr.io/github/github-mcp-server:v1.9.0@sha256:881b53d6f75f69bdbc1b5b10fc2f1361717c19054143b3a8529fb5c32061a50e"}]} # This file was automatically generated by gh-aw (v0.86.2). DO NOT EDIT. To debug this workflow, load the skill at https://github.com/github/gh-aw/blob/main/debug.md # # ___ _ _ @@ -34,12 +34,10 @@ # - actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 # - actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 # - actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 -# - actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # 634f93cb2916e3fdff6788551b99b062d0335ce0 # - actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 # - actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 (source v9) # - actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0 # - actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 -# - actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # ea165f8d65b6e75b540449e92b4886f43607fa02 # - github/gh-aw-actions/setup@6aab9e5b5c91c615506061f09bedd81a23babe3c # v0.86.2 # # Container images used: @@ -104,7 +102,7 @@ jobs: steps: - name: Setup Scripts id: setup - uses: github/gh-aw-actions/setup@be41add0f204de343293628bb99a72a3894cfd7a # v0.88.4 + uses: github/gh-aw-actions/setup@6aab9e5b5c91c615506061f09bedd81a23babe3c # v0.86.2 with: destination: ${{ runner.temp }}/gh-aw/actions job-name: ${{ github.job }} @@ -393,7 +391,7 @@ jobs: steps: - name: Setup Scripts id: setup - uses: github/gh-aw-actions/setup@be41add0f204de343293628bb99a72a3894cfd7a # v0.88.4 + uses: github/gh-aw-actions/setup@6aab9e5b5c91c615506061f09bedd81a23babe3c # v0.86.2 with: destination: ${{ runner.temp }}/gh-aw/actions job-name: ${{ github.job }} @@ -424,7 +422,7 @@ jobs: with: name: activation path: /tmp/gh-aw - - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # 634f93cb2916e3fdff6788551b99b062d0335ce0 + - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 with: name: primer-analysis-input path: /tmp/gh-aw/primer-input @@ -896,7 +894,7 @@ jobs: with: persist-credentials: false ref: ${{ github.workflow_sha }} - - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # 634f93cb2916e3fdff6788551b99b062d0335ce0 + - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 with: github-token: ${{ github.token }} name: mypy_primer_analysis_context @@ -914,7 +912,7 @@ jobs: const source = await loadSource(github.request.bind(github), `${context.repo.owner}/${context.repo.repo}`, handoff); fs.writeFileSync(path.join(process.env.RUNNER_TEMP, 'primer-source.json'), JSON.stringify(source)); return source.runId; - - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # 634f93cb2916e3fdff6788551b99b062d0335ce0 + - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 with: github-token: ${{ github.token }} path: ${{ runner.temp }}/primer-input/raw @@ -940,7 +938,7 @@ jobs: core.setOutput('has_changes', String(manifest.projects.length > 0)); core.notice(`${manifest.projects.length} changed projects; all eight shards accounted for`); - if: steps.prepare.outputs.has_changes == 'true' - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # ea165f8d65b6e75b540449e92b4886f43607fa02 + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 with: if-no-files-found: error name: primer-analysis-input @@ -972,7 +970,7 @@ jobs: steps: - name: Setup Scripts id: setup - uses: github/gh-aw-actions/setup@be41add0f204de343293628bb99a72a3894cfd7a # v0.88.4 + uses: github/gh-aw-actions/setup@6aab9e5b5c91c615506061f09bedd81a23babe3c # v0.86.2 with: destination: ${{ runner.temp }}/gh-aw/actions job-name: ${{ github.job }} @@ -1188,7 +1186,7 @@ jobs: steps: - name: Setup Scripts id: setup - uses: github/gh-aw-actions/setup@be41add0f204de343293628bb99a72a3894cfd7a # v0.88.4 + uses: github/gh-aw-actions/setup@6aab9e5b5c91c615506061f09bedd81a23babe3c # v0.86.2 with: destination: ${{ runner.temp }}/gh-aw/actions job-name: ${{ github.job }} @@ -1450,7 +1448,7 @@ jobs: steps: - name: Setup Scripts id: setup - uses: github/gh-aw-actions/setup@be41add0f204de343293628bb99a72a3894cfd7a # v0.88.4 + uses: github/gh-aw-actions/setup@6aab9e5b5c91c615506061f09bedd81a23babe3c # v0.86.2 with: destination: ${{ runner.temp }}/gh-aw/actions job-name: ${{ github.job }} @@ -1485,13 +1483,13 @@ jobs: with: persist-credentials: false ref: ${{ github.workflow_sha }} - - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # 634f93cb2916e3fdff6788551b99b062d0335ce0 + - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 env: GH_AW_AGENT_OUTPUT: ${{ runner.temp }}/gh-aw/safe-jobs/agent_output.json with: name: primer-analysis-input path: ${{ runner.temp }}/primer-input - - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # 634f93cb2916e3fdff6788551b99b062d0335ce0 + - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 env: GH_AW_AGENT_OUTPUT: ${{ runner.temp }}/gh-aw/safe-jobs/agent_output.json with: @@ -1522,7 +1520,7 @@ jobs: ); core.notice(result); await core.summary.addRaw(result).write(); - - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # ea165f8d65b6e75b540449e92b4886f43607fa02 + - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 env: GH_AW_AGENT_OUTPUT: ${{ runner.temp }}/gh-aw/safe-jobs/agent_output.json with: diff --git a/.github/workflows/mypy-primer-analysis.md b/.github/workflows/mypy-primer-analysis.md index 9e881e68750f..81a9de777b75 100644 --- a/.github/workflows/mypy-primer-analysis.md +++ b/.github/workflows/mypy-primer-analysis.md @@ -44,7 +44,7 @@ jobs: with: ref: ${{ github.workflow_sha }} persist-credentials: false - - uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5 + - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 with: name: mypy_primer_analysis_context run-id: ${{ github.event.workflow_run.id }} @@ -62,7 +62,7 @@ jobs: const source = await loadSource(github.request.bind(github), `${context.repo.owner}/${context.repo.repo}`, handoff); fs.writeFileSync(path.join(process.env.RUNNER_TEMP, 'primer-source.json'), JSON.stringify(source)); return source.runId; - - uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5 + - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 with: pattern: mypy_primer_diffs_* run-id: ${{ steps.source.outputs.result }} @@ -87,14 +87,14 @@ jobs: fs.writeFileSync(path.join(folder, 'manifest.json'), JSON.stringify(manifest, null, 2)); core.setOutput('has_changes', String(manifest.projects.length > 0)); core.notice(`${manifest.projects.length} changed projects; all eight shards accounted for`); - - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 if: steps.prepare.outputs.has_changes == 'true' with: name: primer-analysis-input path: ${{ runner.temp }}/primer-input if-no-files-found: error steps: - - uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5 + - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 with: name: primer-analysis-input path: /tmp/gh-aw/primer-input @@ -123,11 +123,11 @@ safe-outputs: with: ref: ${{ github.workflow_sha }} persist-credentials: false - - uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5 + - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 with: name: primer-analysis-input path: ${{ runner.temp }}/primer-input - - uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5 + - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 with: name: activation path: ${{ runner.temp }}/primer-activation @@ -155,7 +155,7 @@ safe-outputs: ); core.notice(result); await core.summary.addRaw(result).write(); - - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 with: name: mypy-primer-analysis-report path: ${{ runner.temp }}/mypy-primer-analysis.md diff --git a/docs/mypy-primer-analysis.md b/docs/mypy-primer-analysis.md index 58cf03a6687c..3fbfddde4bfb 100644 --- a/docs/mypy-primer-analysis.md +++ b/docs/mypy-primer-analysis.md @@ -43,14 +43,35 @@ They can be adjusted in `.github/workflows/mypy-primer-analysis.md`. The agent m mark insufficiently investigated projects as needing review rather than inventing evidence to meet its budget. -The workflow is compiled with GitHub Agentic Workflows v0.86.2: +The workflow is compiled with GitHub Agentic Workflows v0.86.2. Use that exact +compiler version for regeneration; check the installed version first: ```text +gh aw version gh aw compile mypy-primer-analysis --validate ``` -Commit the Markdown source and generated lock file together; do not edit the lock -file manually. For `gh aw compile --staged`, the publisher reads the staged flag +The compiler owns the `github/gh-aw-actions/setup` runtime as well as the generated +script calls. `.github/aw/actions-lock.json` records its version-to-SHA resolution; +the generated metadata, runtime pin, and compiler version must agree. A standalone +runtime bump can remove scripts that the existing generated workflow still calls +(for example, v0.86.2's `install_ripgrep.sh` is absent from v0.88.4). Dependabot +therefore ignores only `github/gh-aw-actions`; unrelated action updates remain +enabled. The existing primer workflow tests also check compiler/runtime consistency. + +To upgrade, deliberately select the compiler version, regenerate with its matching +runtime, and review the generated diff and resolution lock together. Do not override +the runtime independently or manually edit `.lock.yml`. Keep authored workflow +changes, any resolution-lock changes, and regenerated output together in the same +commit. Check permissions, tools, container/action pins, and the threat-detection +and publisher gates; rerun the primer tests and compile again to confirm stable +output. If Dependabot updated other actions in the generated workflow, reconcile +their authored Markdown pins before regeneration rather than silently downgrading +them. Local compilation does not prove a live comment was posted: `workflow_run` +uses the trusted default-branch workflow, so a PR-only repair cannot fix an existing +run on `main`. + +For `gh aw compile --staged`, the publisher reads the staged flag from the trusted activation artifact: v0.86.2 does not propagate that flag's environment variable into custom output jobs. Missing activation metadata fails closed rather than defaulting to live publication. diff --git a/docs/type-concepts-advanced.md b/docs/type-concepts-advanced.md index 29fb376c72a5..448d02266caa 100644 --- a/docs/type-concepts-advanced.md +++ b/docs/type-concepts-advanced.md @@ -326,17 +326,14 @@ Some functions or methods can return one of several different types. In cases wh 3. If only one overload remains, it is the “winner”. -4. If more than one overload remains, the “winner” is chosen based on the order in which the overloads are declared. In general, the first remaining overload is the “winner”. There are three exceptions to this rule. +4. If more than one overload remains, the “winner” is chosen based on the order in which the overloads are declared. In general, the first remaining overload is the “winner”. There are two exceptions to this rule. Exception 1: When an `*args` (unpacked) argument matches a `*args` parameter in one of the overload signatures, this overrides the normal order-based rule. - Exception 2: When multiple overloads match because an argument contains `Any` or `Unknown` in an invariant type argument, pyright applies the [overload materialization rule](https://typing.python.org/en/latest/spec/overload.html#step-5). An overload that accepts all possible materializations of every argument eliminates all subsequent overloads. If multiple overloads with non-equivalent return types remain, pyright preserves shared generic return structure where possible, replacing incompatible invariant type arguments with `Any` or `Unknown`. Compatible tuple shapes can also be preserved. For example, return types `list[int]` and `list[str]` produce `list[Any]` when the ambiguity comes from a nested `Any`, retaining attribute checking on the list. If no supported common structure exists, the call evaluates to `Any` when the ambiguity is caused by a nested `Any`, or `Unknown` when it is caused by a nested `Unknown`. This also applies to specialized `self` parameters and inferred constructor results. - Exception 3: When two or more overloads match because an argument itself evaluates to `Any` or `Unknown`, the matching overload is ambiguous. In this case, pyright examines the return types of the remaining overloads and eliminates types that are duplicates or are subsumed by (i.e. proper subtypes of) other types in the list. If only one type remains after this coalescing step, that type is used. If more than one type remains after this coalescing step, the type of the call expression evaluates to `Unknown`. For example, if two overloads are matched due to an argument that evaluates to `Any`, and those two overloads have return types of `str` and `LiteralString`, pyright will coalesce this to just `str` because `LiteralString` is a proper subtype of `str`. If the two overloads have return types of `str` and `bytes`, the call expression will evaluate to `Unknown` because `str` and `bytes` have no overlap. + Exception 2: When two or more overloads match because an argument evaluates to `Any` or `Unknown`, the matching overload is ambiguous. In this case, pyright examines the return types of the remaining overloads and eliminates types that are duplicates or are subsumed by (i.e. proper subtypes of) other types in the list. If only one type remains after this coalescing step, that type is used. If more than one type remains after this coalescing step, the type of the call expression evaluates to `Unknown`. For example, if two overloads are matched due to an argument that evaluates to `Any`, and those two overloads have return types of `str` and `LiteralString`, pyright will coalesce this to just `str` because `LiteralString` is a proper subtype of `str`. If the two overloads have return types of `str` and `bytes`, the call expression will evaluate to `Unknown` because `str` and `bytes` have no overlap. 5. If no overloads remain, Pyright considers whether any of the arguments are union types. If so, these union types are expanded into their constituent subtypes, and the entire process of overload matching is repeated with the expanded argument types. If two or more overloads match, the union of their respective return types form the final return type for the call expression. This "union expansion" can result in a combinatoric explosion if many arguments evaluate to union types. For example, if four arguments are present, and they all evaluate to unions that expand to ten subtypes, this could result in 10^4 combinations. Pyright expands unions for arguments left to right and halts expansion when the number of signatures exceeds 64. 6. If no overloads remain and all unions have been expanded, a diagnostic is generated indicating that the supplied arguments are incompatible with all overload signatures. -Binding a method whose `self` parameter is annotated with a generic protocol can infer several correlated parameter and return types. Pyright preserves these as bound overloads so that operations supported by concrete specializations remain available on gradual receivers. If an additional inferred signature becomes the first match, unresolved receiver ambiguity does not allow it to hide later declared overloads. Ordinary protocol argument inference and legacy first-match handling for original signatures remain unchanged. - ### Class and Instance Variables diff --git a/packages/pyright-internal/src/analyzer/constructors.ts b/packages/pyright-internal/src/analyzer/constructors.ts index ffbd63c85a21..fee55fb8a034 100644 --- a/packages/pyright-internal/src/analyzer/constructors.ts +++ b/packages/pyright-internal/src/analyzer/constructors.ts @@ -510,34 +510,18 @@ function validateInitMethod( inferenceContext ? { ...inferenceContext, returnTypeOverride } : undefined ); - // Overload evaluation keeps the ordinary __init__ return as a placeholder - // and carries argument-dependent constructed types through this separate field, - // including when union-expanded argument lists are combined. - const returnType = callResult.specializedInitSelfType - ? mapSubtypes(callResult.specializedInitSelfType, (specializedInitSelfSubtype) => { - let adjustedClassType = type; - if ( - isClassInstance(specializedInitSelfSubtype) && - ClassType.isSameGenericClass(specializedInitSelfSubtype, adjustedClassType) - ) { - adjustedClassType = ClassType.cloneAsInstantiable(specializedInitSelfSubtype); - } - - if ( - !type.priv.isTypeArgExplicit && - (isAny(specializedInitSelfSubtype) || isUnknown(specializedInitSelfSubtype)) - ) { - return specializedInitSelfSubtype; - } - - return applyExpectedTypeForConstructor( - evaluator, - adjustedClassType, - /* inferenceContext */ undefined, - constraints - ); - }) - : applyExpectedTypeForConstructor(evaluator, type, /* inferenceContext */ undefined, constraints); + const returnType = mapSubtypes(callResult.specializedInitSelfType ?? type, (selfType) => { + const adjustedClassType = + isClassInstance(selfType) && ClassType.isSameGenericClass(selfType, type) + ? ClassType.cloneAsInstantiable(selfType) + : type; + return applyExpectedTypeForConstructor( + evaluator, + adjustedClassType, + /* inferenceContext */ undefined, + constraints + ); + }); if (callResult.isTypeIncomplete) { isTypeIncomplete = true; diff --git a/packages/pyright-internal/src/analyzer/protocols.ts b/packages/pyright-internal/src/analyzer/protocols.ts index 00f47ad6725b..f867a467bb9d 100644 --- a/packages/pyright-internal/src/analyzer/protocols.ts +++ b/packages/pyright-internal/src/analyzer/protocols.ts @@ -8,13 +8,12 @@ * (structural subtyping) classes. */ -import { appendArray } from '../common/collectionUtils'; import { assert } from '../common/debug'; import { defaultMaxDiagnosticDepth, DiagnosticAddendum } from '../common/diagnostic'; import { LocAddendum } from '../localization/localize'; import { ConstraintSolution } from './constraintSolution'; import { assignTypeVar } from './constraintSolver'; -import { ConstraintSet, ConstraintTracker } from './constraintTracker'; +import { ConstraintTracker } from './constraintTracker'; import { DeclarationType } from './declaration'; import { assignProperty } from './properties'; import { Symbol } from './symbol'; @@ -823,63 +822,28 @@ function assignToProtocolInternal( // If the dest protocol has type parameters, make sure the source type arguments match. if (typesAreConsistent && destType.shared.typeParams.length > 0) { - // An overloaded member can infer multiple correlated sets of protocol - // type arguments. Specializing a class with all sets uses only the first. + // Create a specialized version of the protocol defined by the dest and + // make sure the resulting type args can be assigned. const genericProtocolType = ClassType.specialize(destType, undefined); - const constraintSets = - (flags & AssignTypeFlags.PreserveProtocolTypeArgs) !== 0 - ? protocolConstraints.getConstraintSets() - : [protocolConstraints.getMainConstraintSet()]; - const matchingConstraints: ConstraintSet[] = []; - const typeArgDiag = constraintSets.length === 1 ? diag : diag ? new DiagnosticAddendum() : undefined; - let typeArgsAreConsistent = false; - - for (const constraintSet of constraintSets) { - const singleProtocolConstraints = new ConstraintTracker(); - singleProtocolConstraints.addConstraintSets([constraintSet]); - const constraintsClone = constraintSets.length === 1 ? constraints : constraints?.clone(); - - if (destType.priv.typeArgs) { - const specializedProtocolType = evaluator.solveAndApplyConstraints( - genericProtocolType, - singleProtocolConstraints - ) as ClassType; + const specializedProtocolType = evaluator.solveAndApplyConstraints( + genericProtocolType, + protocolConstraints + ) as ClassType; - if ( - !evaluator.assignTypeArgs( - destType, - specializedProtocolType, - typeArgDiag, - constraintsClone, - flags, - recursionCount - ) - ) { - continue; - } - } else if (constraintsClone) { - for (const typeParam of destType.shared.typeParams) { - const typeArgEntry = constraintSet.getTypeVar(typeParam); - - if (typeArgEntry) { - constraintsClone.copyBounds(typeArgEntry); - } - } - } - - typeArgsAreConsistent = true; - if (constraintsClone) { - appendArray(matchingConstraints, constraintsClone.getConstraintSets()); + if (destType.priv.typeArgs) { + if ( + !evaluator.assignTypeArgs(destType, specializedProtocolType, diag, constraints, flags, recursionCount) + ) { + typesAreConsistent = false; } - } + } else if (constraints) { + for (const typeParam of destType.shared.typeParams) { + const typeArgEntry = protocolConstraints.getMainConstraintSet().getTypeVar(typeParam); - if (!typeArgsAreConsistent) { - typesAreConsistent = false; - if (typeArgDiag && typeArgDiag !== diag) { - diag?.addAddendum(typeArgDiag); + if (typeArgEntry) { + constraints.copyBounds(typeArgEntry); + } } - } else if (constraints) { - constraints.addConstraintSets(matchingConstraints); } } diff --git a/packages/pyright-internal/src/analyzer/typeEvaluator.ts b/packages/pyright-internal/src/analyzer/typeEvaluator.ts index 408909624ebc..eec2d6b562b9 100644 --- a/packages/pyright-internal/src/analyzer/typeEvaluator.ts +++ b/packages/pyright-internal/src/analyzer/typeEvaluator.ts @@ -454,7 +454,6 @@ interface MatchedOverloadInfo { constraints: ConstraintTracker; argResults: ArgResult[]; returnType: Type; - specializedInitSelfType?: Type; } interface ValidateArgTypeOptions { @@ -10028,16 +10027,12 @@ export function createTypeEvaluator( const specializedInitSelfTypes: Type[] = []; let matchedOverloads: MatchedOverloadInfo[] = []; let isTypeIncomplete = false; - const overloadsUsedForCall: FunctionType[] = []; + let overloadsUsedForCall: FunctionType[] = []; let isDefinitiveMatchFound = false; - let hasInitSelfMaterializationAmbiguity = false; - let hasEffectiveInitSelfType = false; const speculativeNode = getSpeculativeNodeForCall(errorNode); for (let expandedTypesIndex = 0; expandedTypesIndex < expandedArgTypes.length; expandedTypesIndex++) { - const overloadsUsedStartIndex = overloadsUsedForCall.length; let matchedOverload: FunctionType | undefined; - let effectiveInitSelfType: Type | undefined; const argTypeOverride = expandedArgTypes[expandedTypesIndex]; const hasArgTypeOverride = argTypeOverride.some((a) => a !== undefined); let possibleMatchResults: MatchedOverloadInfo[] = []; @@ -10089,15 +10084,10 @@ export function createTypeEvaluator( constraints: effectiveConstraints, returnType: callResult.returnType, argResults: callResult.argResults ?? [], - specializedInitSelfType: callResult.specializedInitSelfType, }; matchedOverloads.push(matchedOverloadInfo); - if ( - callResult.anyOrUnknownArg || - matchResults.unpackedArgOfUnknownLength || - possibleMatchResults.length > 0 - ) { + if (callResult.anyOrUnknownArg || matchResults.unpackedArgOfUnknownLength) { possibleMatchResults.push(matchedOverloadInfo); if (callResult.anyOrUnknownArg) { @@ -10107,7 +10097,25 @@ export function createTypeEvaluator( } } else { returnTypes.push(callResult.returnType); - effectiveInitSelfType = getEffectiveInitSelfType(matchedOverloadInfo); + // Each definitive union branch needs its own constructed type; + // __init__ return types alone cannot represent the specialization. + const boundToType = overload.priv.boundToType; + if ( + expandedArgTypes.length > 1 && + overload.shared.name === '__init__' && + boundToType && + isClassInstance(boundToType) + ) { + specializedInitSelfTypes.push( + callResult.specializedInitSelfType ?? + solveAndApplyConstraints(boundToType, effectiveConstraints, { + replaceUnsolved: { + scopeIds: getTypeVarScopeIds(boundToType), + tupleClassType: getTupleClassType(), + }, + }) + ); + } isDefinitiveMatchFound = true; break; } @@ -10123,62 +10131,21 @@ export function createTypeEvaluator( possibleMatchResults = filterOverloadMatchesForUnpackedArgs(possibleMatchResults); possibleMatchResults = filterOverloadMatchesForAnyArgs(possibleMatchResults); - // Keep diagnostic bookkeeping aligned with the candidates that remain possible. - // This applies to both top-level gradual arguments and nested materialization, - // so deprecation diagnostics are reported only for retained overloads. - overloadsUsedForCall.splice( - overloadsUsedStartIndex, - overloadsUsedForCall.length - overloadsUsedStartIndex, - ...possibleMatchResults.map((result) => result.overload) - ); - // Did the filtering produce a single result? If so, we're done. if (possibleMatchResults.length === 1) { + overloadsUsedForCall = [possibleMatchResults[0].overload]; returnTypes.push(possibleMatchResults[0].returnType); - effectiveInitSelfType = getEffectiveInitSelfType(possibleMatchResults[0]); matchedOverloads = [possibleMatchResults[0]]; } else { - const firstArgParamPairs = getOverloadArgParamPairs(possibleMatchResults[0]); - let ambiguousMatchIncludesNestedAny = false; - let ambiguousMatchIncludesNestedUnknown = false; - let ambiguousMatchIncludesTopLevelAnyOrUnknown = false; - - firstArgParamPairs.forEach((pair, index) => { - const paramTypes = possibleMatchResults.map((match) => { - const argParamPairs = getOverloadArgParamPairs(match); - return index < argParamPairs.length ? argParamPairs[index].paramType : UnknownType.create(); - }); - - if (!areTypesSame(paramTypes, { treatAnySameAsUnknown: true })) { - if (isAnyOrUnknown(pair.argType)) { - ambiguousMatchIncludesTopLevelAnyOrUnknown = true; - } else { - const anyOrUnknown = getAnyOrUnknownInInvariantPosition(pair.argType); - if (anyOrUnknown && isAny(anyOrUnknown)) { - ambiguousMatchIncludesNestedAny = true; - } else if (anyOrUnknown && isUnknown(anyOrUnknown)) { - ambiguousMatchIncludesNestedUnknown = true; - } - } - } - }); - // Eliminate any return types that are subsumed by other return types. let dedupedMatchResults: Type[] = []; let dedupedResultsIncludeAny = false; - const isInitSelfMaterializationAmbiguity = - (ambiguousMatchIncludesNestedAny || ambiguousMatchIncludesNestedUnknown) && - possibleMatchResults.some((result) => !!result.specializedInitSelfType); - - const matchReturnTypes = possibleMatchResults.map((result) => - isInitSelfMaterializationAmbiguity ? getEffectiveOverloadReturnType(result) : result.returnType - ); - matchReturnTypes.forEach((resultType) => { + possibleMatchResults.forEach((result) => { let isSubtypeSubsumed = false; for (let dedupedIndex = 0; dedupedIndex < dedupedMatchResults.length; dedupedIndex++) { - if (assignType(dedupedMatchResults[dedupedIndex], resultType)) { + if (assignType(dedupedMatchResults[dedupedIndex], result.returnType)) { const anyOrUnknown = containsAnyOrUnknown( dedupedMatchResults[dedupedIndex], /* recurse */ false @@ -10189,8 +10156,8 @@ export function createTypeEvaluator( dedupedResultsIncludeAny = true; } break; - } else if (assignType(resultType, dedupedMatchResults[dedupedIndex])) { - const anyOrUnknown = containsAnyOrUnknown(resultType, /* recurse */ false); + } else if (assignType(result.returnType, dedupedMatchResults[dedupedIndex])) { + const anyOrUnknown = containsAnyOrUnknown(result.returnType, /* recurse */ false); if (!anyOrUnknown) { dedupedMatchResults[dedupedIndex] = NeverType.createNever(); } else if (isAny(anyOrUnknown)) { @@ -10201,7 +10168,7 @@ export function createTypeEvaluator( } if (!isSubtypeSubsumed) { - dedupedMatchResults.push(resultType); + dedupedMatchResults.push(result.returnType); } }); @@ -10209,19 +10176,7 @@ export function createTypeEvaluator( const combinedTypes = combineTypes(dedupedMatchResults); let returnType = combinedTypes; - if (ambiguousMatchIncludesNestedUnknown) { - returnType = getCommonGradualReturnType( - matchReturnTypes, - /* useUnknown */ true, - possibleMatchInvolvesIncompleteUnknown - ); - } else if (ambiguousMatchIncludesNestedAny && !ambiguousMatchIncludesTopLevelAnyOrUnknown) { - returnType = getCommonGradualReturnType( - matchReturnTypes, - /* useUnknown */ false, - possibleMatchInvolvesIncompleteUnknown - ); - } else if (dedupedMatchResults.length > 1) { + if (dedupedMatchResults.length > 1) { // If one or more of the deduped types is Any or contains Any, // we will assume that the person who defined the overload really // wanted Any rather than Unknown. In cases where the deduped types @@ -10237,50 +10192,29 @@ export function createTypeEvaluator( } } - if (isInitSelfMaterializationAmbiguity) { - // Overloaded __init__ methods normally return None. Preserve that - // ordinary return as a placeholder while union-expanded calls are - // combined, and carry the effective constructed types separately. - // validateInitMethod consumes specializedInitSelfType to reconstruct - // the constructor result after call validation is complete. - effectiveInitSelfType = returnType; - hasInitSelfMaterializationAmbiguity = true; - returnTypes.push(possibleMatchResults[0].returnType); - } else { - returnTypes.push(returnType); - } + returnTypes.push(returnType); } } - if (effectiveInitSelfType) { - specializedInitSelfTypes.push(effectiveInitSelfType); - hasEffectiveInitSelfType = true; - } - if (!matchedOverload) { return { argumentErrors: true, isTypeIncomplete, overloadsUsedForCall }; } } - // Union expansion requires one constructor handoff per expanded argument list. - // Materialization ambiguity requires a combined handoff even when there is only - // one argument list because multiple overload candidates contribute to its result. - const shouldCombineInitSelfTypes = - hasInitSelfMaterializationAmbiguity || (expandedArgTypes.length > 1 && hasEffectiveInitSelfType); - // We found a match for all of the expanded argument lists. Copy the // resulting type var context back into the caller's type var context. - // Use the type var context from the last matched overload because it - // includes the type var solutions for all earlier matched overloads. + // Constructor results for separate union branches are carried through + // specializedInitSelfTypes rather than the last branch's constraints. if (constraints && isDefinitiveMatchFound) { constraints.copyFromClone(matchedOverloads[matchedOverloads.length - 1].constraints); } // And run through the first expanded argument list one more time to - // populate the type cache. - const finalConstraints = shouldCombineInitSelfTypes - ? matchedOverloads[0].constraints - : constraints ?? matchedOverloads[0].constraints; + // populate the type cache, without applying a different branch's constraints. + const finalConstraints = + expandedArgTypes.length > 1 + ? matchedOverloads[0].constraints + : constraints ?? matchedOverloads[0].constraints; const finalCallResult = validateArgTypesWithContext( errorNode, matchedOverloads[0].matchResults, @@ -10299,117 +10233,13 @@ export function createTypeEvaluator( returnType: combineTypes(returnTypes), isTypeIncomplete, specializedInitSelfType: - specializedInitSelfTypes.length > 0 && shouldCombineInitSelfTypes + specializedInitSelfTypes.length === expandedArgTypes.length ? combineTypes(specializedInitSelfTypes) : finalCallResult.specializedInitSelfType, overloadsUsedForCall, }; } - // Preserve shared generic structure without forming a union, which would reject - // operations supported by only some of the ambiguous overload return types. - function getCommonGradualReturnType( - types: Type[], - useUnknown: boolean, - isTypeIncomplete: boolean, - recursionCount = 0 - ): Type { - const gradualType = useUnknown - ? UnknownType.createPossibleType(combineTypes(types), isTypeIncomplete) - : AnyType.create(); - if (recursionCount > maxTypeRecursionCount) { - return gradualType; - } - - if (areTypesSame(types, {})) { - return types[0]; - } - - if (!types.every(isClassInstance)) { - return gradualType; - } - - const firstType = types[0]; - if ( - !areTypesSame( - types.map((type) => ClassType.specialize(type, undefined)), - {} - ) - ) { - return gradualType; - } - - const combineTypeArgs = (typeArgs: Type[]) => - getCommonGradualReturnType(typeArgs, useUnknown, isTypeIncomplete, recursionCount + 1); - let commonType: ClassType; - - if (isTupleClass(firstType)) { - const tupleTypeArgs = types.map((type) => type.priv.tupleTypeArgs); - const firstTupleTypeArgs = firstType.priv.tupleTypeArgs; - if ( - !firstTupleTypeArgs || - !tupleTypeArgs.every( - (args): args is TupleTypeArg[] => - !!args && - args.length === firstTupleTypeArgs.length && - args.every( - (arg, index) => - arg.isUnbounded === firstTupleTypeArgs[index].isUnbounded && - arg.isOptional === firstTupleTypeArgs[index].isOptional && - !isUnpacked(arg.type) - ) - ) - ) { - return gradualType; - } - - commonType = specializeTupleClass( - firstType, - firstTupleTypeArgs.map((arg, index) => ({ - ...arg, - type: combineTypeArgs(tupleTypeArgs.map((args) => args[index].type)), - })), - /* isTypeArgExplicit */ true, - !!firstType.priv.isUnpacked - ); - } else { - const typeParams = ClassType.getTypeParams(firstType); - const typeArgs = types.map((type) => type.priv.typeArgs); - if ( - typeParams.length === 0 || - typeParams.some((param) => isParamSpec(param) || isTypeVarTuple(param)) || - !typeArgs.every((args): args is Type[] => !!args && args.length === typeParams.length) - ) { - return gradualType; - } - - // Non-invariant receivers still use legacy overload ordering, so widening - // them could reject operations supported by an original specialization. - inferVarianceForClass(firstType); - if ( - typeParams.some( - (param, index) => - TypeVarType.getVariance(param) !== Variance.Invariant && - !areTypesSame( - typeArgs.map((args) => args[index]), - {} - ) - ) - ) { - return gradualType; - } - - commonType = ClassType.specialize( - firstType, - typeParams.map((_, index) => combineTypeArgs(typeArgs.map((args) => args[index]))) - ); - } - - // An alias for one original specialization no longer describes the merged type. - TypeBase.setTypeAliasInfo(commonType, undefined); - return types.every((type) => assignType(type, commonType)) ? commonType : gradualType; - } - // Determines whether one or more overloads can be eliminated because they // rely on an unpacked argument of unknown length when there is at least // one overload that doesn't because it maps to an *args parameter. @@ -10427,339 +10257,6 @@ export function createTypeEvaluator( return unpackedArgsOverloads; } - // assignType cannot be used for this detection because it accepts Any and Unknown - // without exposing whether acceptance depends on a gradual invariant type argument. - function getAnyOrUnknownInInvariantPosition(type: Type, recursionCount = 0): AnyType | UnknownType | undefined { - if (recursionCount > maxTypeRecursionCount) { - return undefined; - } - recursionCount++; - - let result: AnyType | UnknownType | undefined; - const addResult = (newResult: AnyType | UnknownType | undefined) => { - if (newResult) { - result = result ? preserveUnknown(result, newResult) : newResult; - } - }; - - if (isUnion(type)) { - doForEachSubtype(type, (subtype) => { - addResult(getAnyOrUnknownInInvariantPosition(subtype, recursionCount)); - }); - return result; - } - - if (!isClass(type)) { - return undefined; - } - - // Tuple entries are covariant, but they can contain an invariant type. - if (type.priv.tupleTypeArgs) { - type.priv.tupleTypeArgs.forEach((typeArg) => { - addResult(getAnyOrUnknownInInvariantPosition(typeArg.type, recursionCount)); - }); - return result; - } - - if (!type.priv.typeArgs) { - return undefined; - } - - inferVarianceForClass(type); - const typeParams = ClassType.getTypeParams(type); - - type.priv.typeArgs.forEach((typeArg, index) => { - const typeParam = index < typeParams.length ? typeParams[index] : undefined; - const variance = typeParam ? TypeVarType.getVariance(typeParam) : Variance.Invariant; - - if (variance === Variance.Invariant) { - addResult(containsAnyOrUnknown(typeArg, /* recurse */ true)); - } else { - addResult(getAnyOrUnknownInInvariantPosition(typeArg, recursionCount)); - } - }); - - return result; - } - - function getEffectiveOverloadReturnType(match: MatchedOverloadInfo): Type { - return getEffectiveInitSelfType(match) ?? match.returnType; - } - - function getEffectiveInitSelfType(match: MatchedOverloadInfo): Type | undefined { - if (match.specializedInitSelfType) { - return match.specializedInitSelfType; - } - const boundToType = match.overload.priv.boundToType; - if (match.overload.shared.name === '__init__' && boundToType && isClassInstance(boundToType)) { - return solveAndApplyConstraints(boundToType, match.constraints, { - replaceUnsolved: { - scopeIds: getTypeVarScopeIds(boundToType), - tupleClassType: getTupleClassType(), - }, - }); - } - - return undefined; - } - - function getOverloadArgParamPairs(match: MatchedOverloadInfo): { argType: Type; paramType: Type }[] { - const pairs: { argType: Type; paramType: Type }[] = []; - - // argResults and argParams share validation order: supplied arguments in caller - // order, followed by synthesized defaults. Preserve that order so the same index - // across overloads represents the same supplied argument, even for keyword calls. - match.argResults.forEach((argResult, index) => { - const argParam = - index < match.matchResults.argParams.length ? match.matchResults.argParams[index] : undefined; - if (!argParam?.isDefaultArg) { - pairs.push({ - argType: argResult.argType, - paramType: argParam?.paramType ?? UnknownType.create(), - }); - } - }); - - if (match.overload.priv.boundToType && match.overload.priv.strippedFirstParamType) { - pairs.unshift({ - argType: match.overload.priv.boundToType, - paramType: match.overload.priv.strippedFirstParamType, - }); - } - - return pairs; - } - - // assignType tests one gradual source type, whereas overload step 5 asks whether - // every materialization of that source is covered. These helpers therefore use a - // conservative tri-state proof over supported nominal and tuple relationships: - // true means all materializations are covered, false identifies a counterexample, - // and undefined means coverage is unproven. New cases must preserve this invariant. - type MaterializationCoverage = boolean | undefined; - - function combineMaterializationCoverage(results: MaterializationCoverage[]): MaterializationCoverage { - if (results.some((result) => result === false)) { - return false; - } - - return results.some((result) => result === undefined) ? undefined : true; - } - - function areAllMaterializationsEquivalent( - destType: Type, - srcType: Type, - recursionCount = 0 - ): MaterializationCoverage { - if (recursionCount > maxTypeRecursionCount) { - return undefined; - } - recursionCount++; - - if (!containsAnyOrUnknown(srcType, /* recurse */ true)) { - return isTypeSame(destType, srcType, { treatAnySameAsUnknown: true }); - } - - if (isAnyOrUnknown(destType)) { - return true; - } - - if (isTypeSame(destType, srcType, { treatAnySameAsUnknown: true })) { - return true; - } - - if ( - isTypeVar(destType) || - isUnion(destType) || - isFunction(destType) || - isOverloaded(destType) || - (isClass(destType) && ClassType.isProtocolClass(destType)) - ) { - return undefined; - } - - if (isAnyOrUnknown(srcType)) { - return false; - } - - if (isUnion(srcType) || isUnion(destType) || isFunction(srcType) || isFunction(destType)) { - return undefined; - } - - if (!isClass(destType) || !isClass(srcType)) { - return false; - } - - if (!ClassType.isSameGenericClass(destType, srcType)) { - return false; - } - - if (destType.priv.tupleTypeArgs || srcType.priv.tupleTypeArgs) { - const destTupleTypeArgs = destType.priv.tupleTypeArgs; - const srcTupleTypeArgs = srcType.priv.tupleTypeArgs; - if ( - !destTupleTypeArgs || - !srcTupleTypeArgs || - destTupleTypeArgs.length !== srcTupleTypeArgs.length || - destTupleTypeArgs.some( - (destTypeArg, index) => destTypeArg.isUnbounded !== srcTupleTypeArgs[index].isUnbounded - ) - ) { - return false; - } - - return combineMaterializationCoverage( - srcTupleTypeArgs.map((srcTypeArg, index) => - areAllMaterializationsEquivalent(destTupleTypeArgs[index].type, srcTypeArg.type, recursionCount) - ) - ); - } - - const destTypeArgs = destType.priv.typeArgs; - const srcTypeArgs = srcType.priv.typeArgs; - if (!destTypeArgs || !srcTypeArgs || destTypeArgs.length !== srcTypeArgs.length) { - return false; - } - - return combineMaterializationCoverage( - srcTypeArgs.map((srcTypeArg, index) => - areAllMaterializationsEquivalent(destTypeArgs[index], srcTypeArg, recursionCount) - ) - ); - } - - function areAllMaterializationsAssignable( - destType: Type, - srcType: Type, - recursionCount = 0 - ): MaterializationCoverage { - if (recursionCount > maxTypeRecursionCount) { - return undefined; - } - recursionCount++; - - if (!containsAnyOrUnknown(srcType, /* recurse */ true)) { - return assignType(destType, srcType); - } - - if (isAnyOrUnknown(destType)) { - return true; - } - - if (isTypeSame(destType, srcType, { treatAnySameAsUnknown: true })) { - return true; - } - - if ( - isTypeVar(destType) || - isUnion(destType) || - isFunction(destType) || - isOverloaded(destType) || - (isClass(destType) && ClassType.isProtocolClass(destType)) - ) { - return undefined; - } - - if (isAnyOrUnknown(srcType)) { - return assignType(destType, getObjectType()); - } - - if ( - isUnion(srcType) || - isUnion(destType) || - isFunction(srcType) || - isFunction(destType) || - isOverloaded(srcType) || - isOverloaded(destType) - ) { - return undefined; - } - - if (!isClass(destType) || !isClass(srcType)) { - return false; - } - - if (isTupleClass(destType) && isTupleClass(srcType)) { - const destTupleTypeArgs = destType.priv.tupleTypeArgs; - const srcTupleTypeArgs = srcType.priv.tupleTypeArgs; - if (!destTupleTypeArgs || !srcTupleTypeArgs) { - return undefined; - } - - if (destTupleTypeArgs.length === 1 && destTupleTypeArgs[0].isUnbounded) { - return combineMaterializationCoverage( - srcTupleTypeArgs.map((srcTypeArg) => - areAllMaterializationsAssignable(destTupleTypeArgs[0].type, srcTypeArg.type, recursionCount) - ) - ); - } - - if ( - destTupleTypeArgs.length !== srcTupleTypeArgs.length || - destTupleTypeArgs.some( - (destTypeArg, index) => destTypeArg.isUnbounded !== srcTupleTypeArgs[index].isUnbounded - ) - ) { - return false; - } - - return combineMaterializationCoverage( - srcTupleTypeArgs.map((srcTypeArg, index) => - areAllMaterializationsAssignable(destTupleTypeArgs[index].type, srcTypeArg.type, recursionCount) - ) - ); - } - - let specializedSrcType: ClassType | undefined; - if (ClassType.isSameGenericClass(destType, srcType)) { - specializedSrcType = srcType; - } else { - const instantiableDestType = isClassInstance(destType) ? ClassType.cloneAsInstantiable(destType) : destType; - const baseClass = srcType.shared.mro.find( - (mroClass) => isClass(mroClass) && ClassType.isSameGenericClass(instantiableDestType, mroClass) - ); - if (baseClass && isClass(baseClass)) { - specializedSrcType = specializeForBaseClass(srcType, baseClass); - } - } - - if (!specializedSrcType) { - return ClassType.isBuiltIn(destType, 'object') ? true : undefined; - } - - const typeParams = ClassType.getTypeParams(destType); - if (typeParams.length === 0) { - return true; - } - - const destTypeArgs = destType.priv.typeArgs; - const srcTypeArgs = specializedSrcType.priv.typeArgs; - if (!destTypeArgs) { - return true; - } - if (!srcTypeArgs) { - return false; - } - - inferVarianceForClass(destType); - return combineMaterializationCoverage( - srcTypeArgs.map((srcTypeArg, index) => { - const destTypeArg = index < destTypeArgs.length ? destTypeArgs[index] : UnknownType.create(); - const typeParam = index < typeParams.length ? typeParams[index] : undefined; - const variance = typeParam ? TypeVarType.getVariance(typeParam) : Variance.Invariant; - - if (variance === Variance.Covariant) { - return areAllMaterializationsAssignable(destTypeArg, srcTypeArg, recursionCount); - } - - if (variance === Variance.Invariant) { - return areAllMaterializationsEquivalent(destTypeArg, srcTypeArg, recursionCount); - } - - return undefined; - }) - ); - } - // Determines whether multiple incompatible overloads match // due to an Any or Unknown argument type. function filterOverloadMatchesForAnyArgs(matches: MatchedOverloadInfo[]): MatchedOverloadInfo[] { @@ -10767,96 +10264,31 @@ export function createTypeEvaluator( return matches; } - let firstArgParamPairs = getOverloadArgParamPairs(matches[0]); - const hasInvariantAnyOrUnknownArg = firstArgParamPairs.some((pair) => - getAnyOrUnknownInInvariantPosition(pair.argType) - ); - - // If all of the effective return types match, select the first one. + // If all of the return types match, select the first one. if ( areTypesSame( - matches.map((match) => - hasInvariantAnyOrUnknownArg ? getEffectiveOverloadReturnType(match) : match.returnType - ), + matches.map((match) => match.returnType), { treatAnySameAsUnknown: true } ) ) { return [matches[0]]; } - // Apply overload step 5 to arguments that contain a gradual type in an - // invariant position. An overload that covers all materializations - // eliminates only the overloads that follow it. - if (hasInvariantAnyOrUnknownArg) { - let materializationCheckSupported = true; - for (let matchIndex = 0; matchIndex < matches.length; matchIndex++) { - const argParamPairs = getOverloadArgParamPairs(matches[matchIndex]); - const coverage = - argParamPairs.length === firstArgParamPairs.length - ? combineMaterializationCoverage( - argParamPairs.map((pair, index) => { - const argType = firstArgParamPairs[index].argType; - return areAllMaterializationsAssignable(pair.paramType, argType); - }) - ) - : false; - - if (coverage === undefined) { - if (matches[0].overload.priv.isAdditionalProtocolSelfOverload) { - // An additional binding must not hide later declared overloads. - // Preserve legacy inference when an original signature matches first. - continue; - } - - materializationCheckSupported = false; - break; - } - - if (coverage) { - matches = matches.slice(0, matchIndex + 1); - break; - } - } - - if (materializationCheckSupported) { - if (matches.length < 2) { - return matches; - } - - if ( - areTypesSame( - matches.map((match) => getEffectiveOverloadReturnType(match)), - { treatAnySameAsUnknown: true } - ) - ) { - return [matches[0]]; - } - - firstArgParamPairs = getOverloadArgParamPairs(matches[0]); - for (let i = 0; i < firstArgParamPairs.length; i++) { - if (getAnyOrUnknownInInvariantPosition(firstArgParamPairs[i].argType)) { - const paramTypes = matches.map((match) => { - const argParamPairs = getOverloadArgParamPairs(match); - return i < argParamPairs.length ? argParamPairs[i].paramType : UnknownType.create(); - }); - - if (!areTypesSame(paramTypes, { treatAnySameAsUnknown: true })) { - return matches; - } - } - } - } + const firstArgResults = matches[0].argResults; + if (!firstArgResults) { + return matches; } let foundAmbiguousAnyArg = false; - for (let i = 0; i < firstArgParamPairs.length; i++) { + for (let i = 0; i < firstArgResults.length; i++) { // If the arg is Any or Unknown, see if the corresponding // parameter types differ in any way. - if (isAnyOrUnknown(firstArgParamPairs[i].argType)) { - const paramTypes = matches.map((match) => { - const argParamPairs = getOverloadArgParamPairs(match); - return i < argParamPairs.length ? argParamPairs[i].paramType : UnknownType.create(); - }); + if (isAnyOrUnknown(firstArgResults[i].argType)) { + const paramTypes = matches.map((match) => + i < match.matchResults.argParams.length + ? match.matchResults.argParams[i].paramType + : UnknownType.create() + ); if (!areTypesSame(paramTypes, { treatAnySameAsUnknown: true })) { foundAmbiguousAnyArg = true; } @@ -10868,10 +10300,7 @@ export function createTypeEvaluator( // that one of the arguments is an unpacked iterator, and it maps to // an indeterminate number of parameters, which means that the overload // selection is ambiguous. - if ( - foundAmbiguousAnyArg || - matches.some((match) => getOverloadArgParamPairs(match).length !== firstArgParamPairs.length) - ) { + if (foundAmbiguousAnyArg || matches.some((match) => match.argResults.length !== firstArgResults.length)) { return matches; } @@ -13420,9 +12849,7 @@ export function createTypeEvaluator( let argumentErrors = false; let argumentMatchScore = 0; let specializedInitSelfType: Type | undefined; - let anyOrUnknownArg = type.priv.boundToType - ? getAnyOrUnknownInInvariantPosition(type.priv.boundToType) - : undefined; + let anyOrUnknownArg: UnknownType | AnyType | undefined; const speculativeNode = getSpeculativeNodeForCall(errorNode); const typeCondition = getTypeCondition(type); const paramSpec = FunctionType.getParamSpecFromArgsKwargs(type); @@ -13556,11 +12983,10 @@ export function createTypeEvaluator( condition = TypeCondition.combine(condition, argResult.condition) ?? []; } - const argAnyOrUnknown = isAnyOrUnknown(argResult.argType) - ? argResult.argType - : getAnyOrUnknownInInvariantPosition(argResult.argType); - if (argAnyOrUnknown && !argParam.isDefaultArg) { - anyOrUnknownArg = anyOrUnknownArg ? preserveUnknown(argAnyOrUnknown, anyOrUnknownArg) : argAnyOrUnknown; + if (isAnyOrUnknown(argResult.argType)) { + anyOrUnknownArg = anyOrUnknownArg + ? preserveUnknown(argResult.argType, anyOrUnknownArg) + : argResult.argType; } if (paramSpec) { @@ -30169,7 +29595,7 @@ export function createTypeEvaluator( recursionCount: number, firstParamType: ClassType | TypeVarType | undefined, stripFirstParam = true - ): FunctionType | OverloadedType | undefined { + ): FunctionType | undefined { const constraints = new ConstraintTracker(); if (firstParamType) { @@ -30221,11 +29647,7 @@ export function createTypeEvaluator( firstParamType, subDiag?.createAddendum(), constraints, - AssignTypeFlags.AllowUnspecifiedTypeArgs | - (isClass(memberTypeFirstParamType) && - ClassType.isProtocolClass(memberTypeFirstParamType) - ? AssignTypeFlags.PreserveProtocolTypeArgs - : AssignTypeFlags.Default), + AssignTypeFlags.AllowUnspecifiedTypeArgs, recursionCount ) ) { @@ -30265,20 +29687,14 @@ export function createTypeEvaluator( getEffectiveReturnType(memberType); const specializedFunction = solveAndApplyConstraints(memberType, constraints); - if (isFunctionOrOverloaded(specializedFunction)) { - // A protocol-annotated self can infer multiple correlated signatures. - // Preserve them all so call arguments can select the matching one. - const selfParamType = firstParamType ? FunctionType.getParamType(memberType, 0) : undefined; - const hasProtocolSelf = selfParamType && isClass(selfParamType) && ClassType.isProtocolClass(selfParamType); - let isFirstSignature = true; - return mapSignatures(specializedFunction, (signature) => { - const boundSignature = FunctionType.clone(signature, stripFirstParam, baseType); - if (hasProtocolSelf && !isFirstSignature) { - boundSignature.priv.isAdditionalProtocolSelfOverload = true; - } - isFirstSignature = false; - return boundSignature; - }); + if (isFunction(specializedFunction)) { + return FunctionType.clone(specializedFunction, stripFirstParam, baseType); + } + + if (isOverloaded(specializedFunction)) { + // For overloaded functions, use the first overload. This isn't + // strictly correct, but this is an extreme edge case. + return FunctionType.clone(OverloadedType.getOverloads(specializedFunction)[0], stripFirstParam, baseType); } return undefined; diff --git a/packages/pyright-internal/src/analyzer/typeEvaluatorTypes.ts b/packages/pyright-internal/src/analyzer/typeEvaluatorTypes.ts index 28d2547468a5..2733db66fe21 100644 --- a/packages/pyright-internal/src/analyzer/typeEvaluatorTypes.ts +++ b/packages/pyright-internal/src/analyzer/typeEvaluatorTypes.ts @@ -649,10 +649,6 @@ export const enum AssignTypeFlags { // When assigning callables, should a kwargs with an unpacked TypedDict // disallow additional named arguments if it does not have extraItems? DisallowExtraKwargsForTd = 1 << 17, - - // Retain alternative protocol type arguments when specializing a bound - // method so its call arguments can select the appropriate signature. - PreserveProtocolTypeArgs = 1 << 18, } export interface TypeEvaluator { diff --git a/packages/pyright-internal/src/analyzer/typeUtils.ts b/packages/pyright-internal/src/analyzer/typeUtils.ts index 4c8d146fa3a4..a396b944af8f 100644 --- a/packages/pyright-internal/src/analyzer/typeUtils.ts +++ b/packages/pyright-internal/src/analyzer/typeUtils.ts @@ -465,7 +465,7 @@ export function mapSubtypes( // caller to replace one or more signatures with new ones. export function mapSignatures( type: FunctionType | OverloadedType, - callback: (type: FunctionType) => FunctionType | OverloadedType | undefined + callback: (type: FunctionType) => FunctionType | undefined ): OverloadedType | FunctionType | undefined { if (isFunction(type)) { return callback(type); @@ -480,9 +480,7 @@ export function mapSignatures( changeMade = true; } - if (newOverload && isOverloaded(newOverload)) { - newSignatures.push(...OverloadedType.getOverloads(newOverload)); - } else if (newOverload) { + if (newOverload) { newSignatures.push(newOverload); } }); diff --git a/packages/pyright-internal/src/analyzer/types.ts b/packages/pyright-internal/src/analyzer/types.ts index 52653d3f7b0f..7c90204e86aa 100644 --- a/packages/pyright-internal/src/analyzer/types.ts +++ b/packages/pyright-internal/src/analyzer/types.ts @@ -1801,10 +1801,6 @@ export interface FunctionDetailsPriv { // the class or object to which the function was bound. boundToType?: ClassType | undefined; - // An additional signature inferred while binding a protocol-annotated self. - // These must not preempt later declared overloads on an ambiguous receiver. - isAdditionalProtocolSelfOverload?: boolean; - // The flags for the function prior to binding preBoundFlags?: FunctionTypeFlags; diff --git a/packages/pyright-internal/src/tests/mypyPrimerAnalysis.test.ts b/packages/pyright-internal/src/tests/mypyPrimerAnalysis.test.ts index d775f643538f..1b460d74800a 100644 --- a/packages/pyright-internal/src/tests/mypyPrimerAnalysis.test.ts +++ b/packages/pyright-internal/src/tests/mypyPrimerAnalysis.test.ts @@ -679,6 +679,29 @@ describe('mypy_primer analysis', () => { expect(f.request.mock.calls.some(([route]) => /^(POST|PATCH) /.test(route))).toBe(false); }); + test('compiled workflow runtime matches the compiler resolution lock', () => { + const content = readFileSync( + join(__dirname, '../../../../.github/workflows/mypy-primer-analysis.lock.yml'), + 'utf8' + ); + const metadata = parseDocument(content.split('\n')[0].replace('# gh-aw-metadata: ', '')); + const compilerVersion = metadata.get('compiler_version'); + expect(compilerVersion).toMatch(/^v\d+\.\d+\.\d+$/); + const actionsLock = parseDocument( + readFileSync(join(__dirname, '../../../../.github/aw/actions-lock.json'), 'utf8') + ); + const runtimeActions = [...content.matchAll(/^\s+uses: (github\/gh-aw-actions\/[^@\s]+)@(\S+)/gm)]; + expect(runtimeActions.length).toBeGreaterThan(0); + for (const [, repository, sha] of runtimeActions) { + const key = `${repository}@${compilerVersion}`; + const resolvedSha = actionsLock.getIn(['entries', key, 'sha']); + expect(resolvedSha).toMatch(/^[a-f0-9]{40}$/); + expect(actionsLock.getIn(['entries', key, 'repo'])).toBe(repository); + expect(actionsLock.getIn(['entries', key, 'version'])).toBe(compilerVersion); + expect(sha).toBe(resolvedSha); + } + }); + test('compiled workflow keeps the agent read-only and gates publication on threat detection', () => { const workflow = parseDocument( readFileSync(join(__dirname, '../../../../.github/workflows/mypy-primer-analysis.lock.yml'), 'utf8') diff --git a/packages/pyright-internal/src/tests/samples/constructor34.py b/packages/pyright-internal/src/tests/samples/constructor34.py new file mode 100644 index 000000000000..448514bf9e52 --- /dev/null +++ b/packages/pyright-internal/src/tests/samples/constructor34.py @@ -0,0 +1,116 @@ +# This sample checks that replaying a union-expanded constructor call does not +# apply one mapping branch's type variable constraints to another branch. + +from collections.abc import Iterable, Mapping, MutableMapping +from typing import Generic, TypeVar, assert_type, overload + + +class CacheControl(MutableMapping[str, str | None]): + _data: MutableMapping[str, str | None] + + def __init__( + self, + values: Mapping[str, str | None] | Iterable[tuple[str, str | None]] | None = None, + ) -> None: + if values is None: + values = {} + elif isinstance(values, Mapping): + if not isinstance(values, MutableMapping): + values = dict(values) + else: + values = dict(values) + + # This should generate an error: the narrowed union still includes mappings + # whose keys are pairs, which cannot be assigned to this string-keyed field. + self._data = values + + +def concrete_mapping(values: Mapping[str, str | None]) -> None: + result = dict(values) + assert_type(result, dict[str, str | None]) + result["new"] = None + result["other"] = "value" + assert_type(result.get("missing"), str | None) + + # This should generate an error: the key must remain str. + result[1] = None + # This should generate an error: the value must remain str | None. + result["bad"] = 1 + # This should generate an error: construction must not erase the dict type. + result.nonexistent_member() + + +def iterable_pairs(values: Iterable[tuple[str, int]]) -> None: + result = dict(values) + assert_type(result, dict[str, int]) + assert_type(result["key"].bit_length(), int) + + +def optional_mapping(values: Mapping[str, int] | None = None) -> None: + if values is None: + values = {} + result = dict(values) + assert_type(result, dict[str, int]) + + +def mapping_union(values: Mapping[str, int] | Mapping[int, str]) -> None: + result = dict(values) + assert_type(result, dict[str, int] | dict[int, str]) + for value in result.values(): + assert_type(value, int | str) + + contextual: Mapping[str, int] | Mapping[int, str] = dict(values) + assert_type(contextual, dict[str, int] | dict[int, str]) + + factory = dict + assert_type(factory(values), dict[str, int] | dict[int, str]) + + +def reversed_union(values: Mapping[int, str] | Mapping[str, int]) -> None: + assert_type(dict(values), dict[int, str] | dict[str, int]) + + +def different_overloads(values: Mapping[str, int] | Iterable[tuple[int, str]]) -> None: + assert_type(dict(values), dict[str, int] | dict[int, str]) + + +def explicit_specialization(values: Mapping[str, int] | Iterable[tuple[str, int]]) -> None: + assert_type(dict[str, int](values), dict[str, int]) + + +T = TypeVar("T") + + +class Box(Generic[T]): + @overload + def __init__(self, values: list[T]) -> None: ... + @overload + def __init__(self, values: set[T]) -> None: ... + def __init__(self, values: list[T] | set[T]) -> None: ... + + def get(self) -> T: ... + + +class SpecializedBox(Generic[T]): + @overload + def __init__(self: "SpecializedBox[int]", values: list[int]) -> None: ... + @overload + def __init__(self: "SpecializedBox[str]", values: set[str]) -> None: ... + def __init__(self, values: list[int] | set[str]) -> None: ... + + def get(self) -> T: ... + + +def overloaded_init(values: list[int] | set[str]) -> None: + box = Box(values) + assert_type(box, Box[int] | Box[str]) + assert_type(box.get(), int | str) + contextual: Box[int] | Box[str] = Box(values) + assert_type(contextual, Box[int] | Box[str]) + + specialized = SpecializedBox(values) + assert_type(specialized, SpecializedBox[int] | SpecializedBox[str]) + assert_type(specialized.get(), int | str) + + # This should generate two errors: an explicit int specialization rejects set[str]. + Box[int](values) diff --git a/packages/pyright-internal/src/tests/samples/overloadCall11.py b/packages/pyright-internal/src/tests/samples/overloadCall11.py index 97acd795c423..b0c9714f4ed3 100644 --- a/packages/pyright-internal/src/tests/samples/overloadCall11.py +++ b/packages/pyright-internal/src/tests/samples/overloadCall11.py @@ -1,5 +1,5 @@ -# This sample tests overload matching when an invariant container is -# specialized with Any. +# This sample records the known conformance limitation restored by rolling back +# #11601/#11732, not the required semantics of an ambiguous invariant-container call. from typing import Any, overload @@ -19,4 +19,14 @@ def overloaded(value: Any) -> list[Any]: def check(value: list[Any]) -> None: - reveal_type(overloaded(value), expected_text="list[Any]") + result = overloaded(value) + reveal_type(result, expected_text="list[int]") + + # Conformance requires assignability to BOTH retained return types and permits + # operations supported by either. First-match inference currently fails these checks. + int_result: list[int] = result + # This should generate an error: the known rollback limitation rejects list[str]. + str_result: list[str] = result + result.append(1) + # This should generate an error: string append is valid for a retained materialization. + result.append("value") diff --git a/packages/pyright-internal/src/tests/samples/overloadCall12.py b/packages/pyright-internal/src/tests/samples/overloadCall12.py index 4a05ccb9e1a9..a13b9d026218 100644 --- a/packages/pyright-internal/src/tests/samples/overloadCall12.py +++ b/packages/pyright-internal/src/tests/samples/overloadCall12.py @@ -1,5 +1,5 @@ -# This sample tests overload materialization using patterns found in -# real-world libraries and applications. +# This sample records known limitations restored by the overload rollback +# (#11601/#11621/#11732), alongside unchanged concrete and unambiguous controls. import re from collections.abc import Callable, Sequence @@ -44,10 +44,11 @@ def check_pandas_operators( index_unknown: Index, index_str: Index[str], ) -> None: - reveal_type(series_any + 1, expected_text="Series[Any]") - reveal_type(series_unknown + 1, expected_text="Series[Unknown]") - reveal_type(index_any * 2, expected_text="Index[Any]") - reveal_type(index_unknown * 2, expected_text="Index[Unknown]") + # Known rollback limitation (#11601/#11732): gradual self loses the str alternatives. + reveal_type(series_any + 1, expected_text="Series[int]") + reveal_type(series_unknown + 1, expected_text="Series[int]") + reveal_type(index_any * 2, expected_text="Index[int]") + reveal_type(index_unknown * 2, expected_text="Index[int]") concrete_series = series_int + 1 reveal_type(concrete_series, expected_text="Series[int]") @@ -70,8 +71,9 @@ def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> Any: def check_flow(flow_any: Flow[[], Any], flow_unknown: Flow, flow_int: Flow[[], int]) -> None: - reveal_type(flow_any(), expected_text="Any") - reveal_type(flow_unknown(), expected_text="Unknown") + # Known rollback limitation (#11601): the Never-specialized first signature wins. + reveal_type(flow_any(), expected_text="None") + reveal_type(flow_unknown(), expected_text="None") reveal_type(flow_int(), expected_text="int") reveal_type(flow_int() + 1, expected_text="int") @@ -96,7 +98,8 @@ def check_optional_mapper( mapper_int: EnumMapper[int, str], value: Any, ) -> None: - reveal_type(mapper_any.from_wire(value), expected_text="str") + # Known rollback limitation (#11601): optional/top-level Any matching retains None. + reveal_type(mapper_any.from_wire(value), expected_text="str | None") reveal_type(mapper_unknown.from_wire(value), expected_text="Unknown") reveal_type(mapper_int.from_wire(1), expected_text="str") reveal_type(mapper_int.from_wire(1).upper(), expected_text="str") @@ -128,8 +131,9 @@ def check_shape_overloads( rotations_unknown: list[Rotation], rotations_scalar: list[Rotation[tuple[()]]], ) -> None: - reveal_type(Rotation.concatenate(rotations_any), expected_text="Rotation[Any]") - reveal_type(Rotation.concatenate(rotations_unknown), expected_text="Rotation[Unknown]") + # Known rollback limitation (#11601/#11732): other possible shapes are discarded. + reveal_type(Rotation.concatenate(rotations_any), expected_text="Rotation[tuple[int]]") + reveal_type(Rotation.concatenate(rotations_unknown), expected_text="Rotation[tuple[int]]") reveal_type(Rotation.concatenate(rotations_scalar), expected_text="Rotation[tuple[int]]") @@ -139,10 +143,11 @@ def check_pattern( pattern_str: re.Pattern[str], text: str, ) -> None: - reveal_type(pattern.match(text), expected_text="Unknown") - reveal_type(pattern.sub("", text), expected_text="Unknown") - reveal_type(pattern_any.match(text), expected_text="Any") - reveal_type(pattern_any.sub("", text), expected_text="Any") + # Known rollback limitation (#11601): gradual Pattern self selects the str signatures. + reveal_type(pattern.match(text), expected_text="Match[str] | None") + reveal_type(pattern.sub("", text), expected_text="str") + reveal_type(pattern_any.match(text), expected_text="Match[str] | None") + reveal_type(pattern_any.sub("", text), expected_text="str") reveal_type(pattern_str.match(text), expected_text="Match[str] | None") reveal_type(pattern_str.sub("", text), expected_text="str") @@ -204,8 +209,9 @@ def __init__(self, values: list[Any] | set[Any]) -> None: def check_constructors(values_any: list[Any], values_unknown: list, values_int: list[int]) -> None: - reveal_type(Table(values_any), expected_text="Table[Any]") - reveal_type(Table(values_unknown), expected_text="Table[Unknown]") + # Known rollback limitation (#11601/#11732): only the first inferred self is retained. + reveal_type(Table(values_any), expected_text="Table[int]") + reveal_type(Table(values_unknown), expected_text="Table[int]") reveal_type(Table(values_int), expected_text="Table[int]") reveal_type(Table[int](values_any), expected_text="Table[int]") @@ -222,14 +228,17 @@ def check_constructor_union( values_partially_specialized: list[Any] | set[float], values_inferred: list[int] | set[str], ) -> None: - reveal_type(Table(values_any), expected_text="Table[Any] | Table[bytes]") - reveal_type(Table(values_unknown), expected_text="Table[Unknown] | Table[bytes]") + # Union-expanded constructors preserve each branch and its own constraints. + # Known rollback limitation (#11601/#11732): each gradual list still selects int, not str. + reveal_type(Table(values_any), expected_text="Table[int] | Table[bytes]") + reveal_type(Table(values_unknown), expected_text="Table[int] | Table[bytes]") reveal_type(Table(values_int), expected_text="Table[int] | Table[bytes]") - reveal_type(AmbiguousTable(values_gradual), expected_text="AmbiguousTable[Any] | AmbiguousTable[Unknown]") + reveal_type(AmbiguousTable(values_gradual), expected_text="AmbiguousTable[int]") reveal_type( PartiallySpecializedTable(values_partially_specialized), - expected_text="PartiallySpecializedTable[Any] | PartiallySpecializedTable[float]", + expected_text="PartiallySpecializedTable[int] | PartiallySpecializedTable[float]", ) + # Replaying one branch must not reuse another branch's incompatible constraints. reveal_type(InferredTable(values_inferred), expected_text="InferredTable[int] | InferredTable[str]") reveal_type(table.__init__(values_any), expected_text="None") @@ -351,9 +360,10 @@ def keyword_default_case(value: list[Any], *, name: str, scale: int = 0) -> int def check_keyword_default(values_any: list[Any], values_unknown: list) -> None: - reveal_type(keyword_default_case(name="value", value=values_any), expected_text="Any") - reveal_type(keyword_default_case(name="value", value=values_unknown), expected_text="Unknown") - reveal_type(keyword_default_case(name="value", value=values_any, scale=1), expected_text="Any") + # Known rollback limitation (#11601): keyword/default alignment no longer materializes list elements. + reveal_type(keyword_default_case(name="value", value=values_any), expected_text="int") + reveal_type(keyword_default_case(name="value", value=values_unknown), expected_text="int") + reveal_type(keyword_default_case(name="value", value=values_any, scale=1), expected_text="int") _TBound = TypeVar("_TBound", bound=int) @@ -505,8 +515,9 @@ def check_supported_and_unsupported_shapes( reveal_type(callable_case(values_unknown), expected_text="int") reveal_type(union_case(values_any), expected_text="int") reveal_type(union_case(values_unknown), expected_text="int") - reveal_type(tuple_case(tuple_values), expected_text="Any") - reveal_type(tuple_case(tuple_unknown_values), expected_text="Unknown") + # Known rollback limitation (#11601): nested tuple materializations select the int branch. + reveal_type(tuple_case(tuple_values), expected_text="int") + reveal_type(tuple_case(tuple_unknown_values), expected_text="int") reveal_type(tuple_union_case(tuple_union_values), expected_text="int") @@ -548,6 +559,6 @@ def deprecated_case(value: list[Any]) -> int | str: def check_deprecated(values: list[Any], string_values: list[str]) -> None: - # This should generate a deprecation warning. - reveal_type(deprecated_case(values), expected_text="Any") + # Known rollback limitation (#11601): first-match int inference still reports deprecation. + reveal_type(deprecated_case(values), expected_text="int") reveal_type(deprecated_case(string_values), expected_text="str") diff --git a/packages/pyright-internal/src/tests/samples/overloadCall13.py b/packages/pyright-internal/src/tests/samples/overloadCall13.py index 6537e72db99b..c438999649e8 100644 --- a/packages/pyright-internal/src/tests/samples/overloadCall13.py +++ b/packages/pyright-internal/src/tests/samples/overloadCall13.py @@ -1,5 +1,6 @@ -# This sample tests preservation of generic return types when overloads are -# ambiguous because of Any or Unknown, including specialized self parameters. +# This sample records known limitations restored by rolling back #11601/#11732. +# Ambiguous results should permit assignments and operations supported by either +# retained materialization. The expected first-match types below do not meet that requirement. from typing import Any, Generic, Never, TypeAlias, TypeVar, assert_type, overload @@ -27,9 +28,10 @@ def check_copy(value: Container[Any], unknown: Container, concrete: Container[in int_result: Container[int] = result float_result: Container[float] = result + # Known rollback limitation: the first Never-specialized self erases Unknown to Any. unknown_result = unknown.copy() - reveal_type(unknown_result, expected_text="Container[Unknown]") - reveal_type(unknown_result.item(), expected_text="Unknown") + reveal_type(unknown_result, expected_text="Container[Any]") + reveal_type(unknown_result.item(), expected_text="Any") assert_type(concrete.copy(), Container[int]) assert_type(concrete.copy().item(), int) @@ -53,13 +55,16 @@ def nested(value: Any) -> dict[str, list[Any]]: def check_nested(value: list[Any], unknown: list) -> None: result = nested(value) - assert_type(result, dict[str, list[Any]]) + # Known rollback limitation: only the int materialization survives. + assert_type(result, dict[str, list[int]]) int_result: IntResult = result + # This should generate an error despite str being a retained materialization. str_result: StrResult = result result["key"].append(1) + # This should generate an error: the rollback rejects a supported mutation. result["key"].append("value") - assert_type(result["key"][0], Any) - reveal_type(nested(unknown), expected_text="dict[str, list[Unknown]]") + assert_type(result["key"][0], int) + reveal_type(nested(unknown), expected_text="dict[str, list[int]]") # This should generate an error for the nonexistent attribute. result["key"].nonexistent_member() @@ -75,11 +80,14 @@ def tuple_result(value: Any) -> tuple[str, list[Any]]: def check_tuple(value: list[Any]) -> None: result = tuple_result(value) - assert_type(result, tuple[str, list[Any]]) + # Known rollback limitation: tuple structure remains, but its list loses str. + assert_type(result, tuple[str, list[int]]) int_result: tuple[str, list[int]] = result + # This should generate an error despite str being a retained materialization. str_result: tuple[str, list[str]] = result result[0].upper() result[1].append(1) + # This should generate an error: the rollback rejects a supported mutation. result[1].append("value") # This should generate an error for the nonexistent attribute. @@ -127,16 +135,26 @@ def never_result(value: Any) -> Any: def check_fallbacks(value: list[Any], unknown: list) -> None: - assert_type(different_shapes(value), Any) - assert_type(different_families(value), Any) - assert_type(unconstrained_result(value), Any) - assert_type(optional_result(value), Any) - assert_type(never_result(value), Any) - reveal_type(different_shapes(unknown), expected_text="Unknown") - reveal_type(different_families(unknown), expected_text="Unknown") - reveal_type(unconstrained_result(unknown), expected_text="Unknown") - reveal_type(optional_result(unknown), expected_text="Unknown") - reveal_type(never_result(unknown), expected_text="Unknown") + # Known rollback limitation: unsupported common shapes also select only the first result. + assert_type(different_shapes(value), tuple[int]) + assert_type(different_families(value), list[int]) + assert_type(unconstrained_result(value), list[int]) + assert_type(optional_result(value), list[int] | None) + reveal_type(different_shapes(unknown), expected_text="tuple[int]") + reveal_type(different_families(unknown), expected_text="list[int]") + reveal_type(unconstrained_result(unknown), expected_text="list[int]") + reveal_type(optional_result(unknown), expected_text="list[int] | None") + + +# Keep Never calls in separate functions so neither hides the other Unknown, +# fallback, or operation controls as unreachable after the rollback. +def check_never_any(value: list[Any]) -> None: + # Known rollback limitation: a possible list[str] result is incorrectly treated as Never. + reveal_type(never_result(value), expected_text="Never") + + +def check_never_unknown(unknown: list) -> None: + reveal_type(never_result(unknown), expected_text="Never") class Constructed(Generic[_T]): @@ -153,9 +171,10 @@ def item(self) -> _T: def check_constructor(value: list[Any], unknown: list) -> None: result = Constructed(value) - assert_type(result, Constructed[Any]) - assert_type(result.item(), Any) - reveal_type(Constructed(unknown), expected_text="Constructed[Unknown]") + # Known rollback limitation: inferred construction drops the str alternative. + assert_type(result, Constructed[int]) + assert_type(result.item(), int) + reveal_type(Constructed(unknown), expected_text="Constructed[int]") assert_type(Constructed[int](value), Constructed[int]) # This should generate an error for the nonexistent attribute. @@ -183,10 +202,13 @@ def covariant_result(value: Any) -> Any: def check_covariant(value: list[Any], unknown: list, concrete: Covariant[str]) -> None: - assert_type(covariant_result(value), Any) - reveal_type(covariant_result(unknown), expected_text="Unknown") + # Known rollback limitation: first-match Covariant[int] rejects operations on Covariant[str]. + assert_type(covariant_result(value), Covariant[int]) + reveal_type(covariant_result(unknown), expected_text="Covariant[int]") + # This should generate an error despite str.upper being supported by a materialization. covariant_result(value).get().upper() covariant_result(value).get().bit_length() + # This should generate the same error for the independently checked Unknown path. covariant_result(unknown).get().upper() covariant_result(unknown).get().bit_length() assert_type(concrete.get(), str) diff --git a/packages/pyright-internal/src/tests/samples/overloadCall14.py b/packages/pyright-internal/src/tests/samples/overloadCall14.py index 913a4a7a788c..2c46452ca7cb 100644 --- a/packages/pyright-internal/src/tests/samples/overloadCall14.py +++ b/packages/pyright-internal/src/tests/samples/overloadCall14.py @@ -1,5 +1,6 @@ -# This sample tests preservation of correlated overloads inferred from a -# protocol-annotated self parameter, including on gradual generic receivers. +# This sample records the known loss of correlated protocol-self alternatives +# after rolling back #11732. Valid operations on either concrete materialization +# should remain supported; the current errors below document the limitation. from __future__ import annotations @@ -69,36 +70,45 @@ def check( ) -> None: assert_type(concrete.op("value"), Container[int]) assert_type(gradual._proto("value"), ItemOps[int]) - assert_type(gradual.op("value"), Container[int]) + # Each str argument below should generate a current error: only the first (int, str) + # protocol binding survives on gradual receivers. Its return is not proof of a valid call. + assert_type(gradual.op("value"), Container[str]) assert_type(gradual.op(1), Container[str]) - assert_type(unknown.op("value"), Container[int]) + assert_type(unknown.op("value"), Container[str]) assert_type(unknown.op(1), Container[str]) - assert_type(choose(value), Container[Any]) - assert_type(choose(value).op("value"), Container[int]) + # Known rollback limitation (#11601/#11732): choose also drops the str receiver. + assert_type(choose(value), Container[int]) + assert_type(choose(value).op("value"), Container[str]) assert_type(choose(value).op(1), Container[str]) - assert_type(Container.op(gradual, "value"), Container[int]) + assert_type(Container.op(gradual, "value"), Container[str]) - assert_type(gradual.converted("value"), Container[int]) + # These valid str operations remain rejected, including keyword-selected and stored methods. + assert_type(gradual.converted("value"), Container[str]) assert_type(gradual.converted(1), Container[str]) - assert_type(gradual.converted("value", wrapped=False), int) + # This should generate two errors because the second correlated binding was lost. + reveal_type(gradual.converted("value", wrapped=False), expected_text="Unknown") assert_type(gradual.converted(1, wrapped=False), str) bound = gradual.converted - assert_type(bound("value", wrapped=False), int) + # This should generate the same two errors through the stored bound method. + reveal_type(bound("value", wrapped=False), expected_text="Unknown") assert_type(bound(1, wrapped=False), str) - assert_type(choose(value) // "value", Container[int]) + # This should generate an operator error for a valid str materialization. + reveal_type(choose(value) // "value", expected_text="Unknown") assert_type(choose(value) // 1, Container[str]) assert_type(choose(value) // b"value", Container[bytes]) assert_type(concrete.mixed("value"), Container[int]) - assert_type(gradual.mixed("value"), Container[Any]) + # Known rollback limitation: the later declared overload wins after losing the str binding. + assert_type(gradual.mixed("value"), Container[bytes]) assert_type(infer_result(gradual), str) - # This should generate two errors because no overload accepts bytes. + # This should generate an argument error because no protocol binding accepts bytes. gradual.op(b"invalid") # This should generate an error because the concrete receiver requires str. concrete.op(1) - # This should generate an error because the container type is preserved. + # This should generate the current str-argument error AND a missing-member error; + # rollback inference preserves a container, but incorrectly selects Container[str]. choose(value).op("value").nonexistent_member() # This should generate an error because no operator overload accepts object. diff --git a/packages/pyright-internal/src/tests/samples/overloadCall6.py b/packages/pyright-internal/src/tests/samples/overloadCall6.py index dea57e740b14..6d236e127f65 100644 --- a/packages/pyright-internal/src/tests/samples/overloadCall6.py +++ b/packages/pyright-internal/src/tests/samples/overloadCall6.py @@ -124,11 +124,13 @@ def overload5(x: list[str] | list[int]) -> list[str] | list[int]: def func6(y: list[Any]): - reveal_type(overload5(y), expected_text="list[Any]") + # Known rollback limitation (#11601/#11732): first-match inference omits str materializations. + reveal_type(overload5(y), expected_text="list[int]") def func6_unknown(y: list): - reveal_type(overload5(y), expected_text="list[Unknown]") + # The same rollback limitation applies to nested Unknown. + reveal_type(overload5(y), expected_text="list[int]") @overload @@ -176,7 +178,8 @@ def overload5_covariant_base(x: Sequence[object]) -> object: def func6_covariant_base(y: list[Any]): - reveal_type(overload5_covariant_base(y), expected_text="Any") + # Known rollback limitation (#11601): Sequence[int] does not cover every materialization. + reveal_type(overload5_covariant_base(y), expected_text="int") @overload @@ -242,7 +245,8 @@ def overload5_unknown(x: list[Any]) -> int: def func6_nested_unknown(x: list): - reveal_type(overload5_unknown(x), expected_text="Unknown") + # Known rollback limitation (#11601): the first literal return excludes later results. + reveal_type(overload5_unknown(x), expected_text="Literal[1]") @overload @@ -299,7 +303,8 @@ def __init__(self, value: list[Any]) -> None: def func6_overloaded_init(x: list[Any]): - reveal_type(ClassWithOverloadedInit(x), expected_text="ClassWithOverloadedInit[Any]") + # Known rollback limitation (#11601/#11732): inferred self selects only the int constructor. + reveal_type(ClassWithOverloadedInit(x), expected_text="ClassWithOverloadedInit[int]") class ExplicitClassWithOverloadedInit(Generic[_T]): @@ -344,7 +349,8 @@ def m1(self) -> "ClassA[Any]": def func7(a: ClassA[Any]): - reveal_type(a.m1(), expected_text="ClassA[Any]") + # Known rollback limitation (#11601/#11732): gradual self no longer retains both results. + reveal_type(a.m1(), expected_text="ClassA[int]") class ClassB(Generic[_T]): @@ -359,7 +365,8 @@ def m1(self, obj: Any) -> "ClassB[Any]": def func8(b: ClassB[Any]): - reveal_type(b.m1(b), expected_text="ClassB[Any]") + # Known rollback limitation (#11601/#11732): gradual self and argument select the int branch. + reveal_type(b.m1(b), expected_text="ClassB[int]") _T1 = TypeVar("_T1") @@ -478,7 +485,8 @@ def overload10(x) -> Any: def func18(a: Any, b: list[Any], c: list[str], d: list[int]): reveal_type(overload10(a), expected_text="list[int]") - reveal_type(overload10(b), expected_text="list[Any]") + # Known rollback limitation (#11601/#11732): the later covering overload is not retained. + reveal_type(overload10(b), expected_text="list[int]") reveal_type(overload10(c), expected_text="list[Any]") reveal_type(overload10(d), expected_text="list[int]") diff --git a/packages/pyright-internal/src/tests/typeEvaluator6.test.ts b/packages/pyright-internal/src/tests/typeEvaluator6.test.ts index 80b2cf90df56..1d8641a98e96 100644 --- a/packages/pyright-internal/src/tests/typeEvaluator6.test.ts +++ b/packages/pyright-internal/src/tests/typeEvaluator6.test.ts @@ -70,8 +70,8 @@ test('OverloadCall6', () => { const analysisResults = TestUtils.typeAnalyzeSampleFiles(['overloadCall6.py']); TestUtils.validateResults(analysisResults, 2, 0, undefined, undefined, undefined, 1); assert.deepStrictEqual(analysisResults[0].deprecateds[0].range, { - start: { line: 286, character: 16 }, - end: { line: 286, character: 42 }, + start: { line: 290, character: 16 }, + end: { line: 290, character: 42 }, }); }); @@ -97,45 +97,144 @@ test('OverloadCall10', () => { test('OverloadCall11', () => { const analysisResults = TestUtils.typeAnalyzeSampleFiles(['overloadCall11.py']); - TestUtils.validateResults(analysisResults, 0); + TestUtils.validateResults(analysisResults, 2); + // These diagnostics record the restored conformance gap, not the required overload semantics. + assert.deepStrictEqual( + analysisResults[0].errors.map((diagnostic) => ({ + rule: diagnostic.getRule(), + range: diagnostic.range, + message: diagnostic.message, + })), + [ + { + rule: DiagnosticRule.reportAssignmentType, + range: { start: { line: 28, character: 28 }, end: { line: 28, character: 34 } }, + message: + 'Type "list[int]" is not assignable to declared type "list[str]"\n' + + '\u00a0\u00a0"list[int]" is not assignable to "list[str]"\n' + + '\u00a0\u00a0\u00a0\u00a0Type parameter "_T@list" is invariant, but "int" is not the same as "str"\n' + + '\u00a0\u00a0\u00a0\u00a0Consider switching from "list" to "Sequence" which is covariant', + }, + { + rule: DiagnosticRule.reportArgumentType, + range: { start: { line: 31, character: 18 }, end: { line: 31, character: 25 } }, + message: + 'Argument of type "Literal[\'value\']" cannot be assigned to parameter "object" of type "int" in function "append"\n' + + '\u00a0\u00a0"Literal[\'value\']" is not assignable to "int"', + }, + ] + ); }); test('OverloadCall12', () => { const analysisResults = TestUtils.typeAnalyzeSampleFiles(['overloadCall12.py']); TestUtils.validateResults(analysisResults, 5, 0, undefined, undefined, undefined, 1); + // Union-expanded constructors must not add errors to these unrelated negative controls. + assert.deepStrictEqual( + analysisResults[0].errors.map((diagnostic) => [diagnostic.range.start.line + 1, diagnostic.getRule()]), + [ + [59, DiagnosticRule.reportOperatorIssue], + [315, DiagnosticRule.reportCallIssue], + [315, DiagnosticRule.reportArgumentType], + [81, DiagnosticRule.reportCallIssue], + [219, DiagnosticRule.reportArgumentType], + ] + ); }); test('OverloadCall13', () => { const analysisResults = TestUtils.typeAnalyzeSampleFiles(['overloadCall13.py']); - TestUtils.validateResults(analysisResults, 6); - const expectedClasses = [ - 'Container[Any]', - 'Container[Unknown]', - 'Container[int]', - 'list[Any]', - 'list[Any]', - 'Constructed[Any]', + TestUtils.validateResults(analysisResults, 12, 0, 11, undefined, 0); + // Both Never calls and the previously unreachable Unknown fallbacks must be analyzed. + assert.deepStrictEqual( + analysisResults[0].infos.map((diagnostic) => diagnostic.message), + [ + 'Type of "unknown_result" is "Container[Any]"', + 'Type of "unknown_result.item()" is "Any"', + 'Type of "nested(unknown)" is "dict[str, list[int]]"', + 'Type of "different_shapes(unknown)" is "tuple[int]"', + 'Type of "different_families(unknown)" is "list[int]"', + 'Type of "unconstrained_result(unknown)" is "list[int]"', + 'Type of "optional_result(unknown)" is "list[int] | None"', + 'Type of "never_result(value)" is "Never"', + 'Type of "never_result(unknown)" is "Never"', + 'Type of "Constructed(unknown)" is "Constructed[int]"', + 'Type of "covariant_result(unknown)" is "Covariant[int]"', + ] + ); + assert.deepStrictEqual( + analysisResults[0].errors.map((diagnostic) => [diagnostic.range.start.line + 1, diagnostic.getRule()]), + [ + [39, DiagnosticRule.reportAttributeAccessIssue], + [40, DiagnosticRule.reportAttributeAccessIssue], + [41, DiagnosticRule.reportAttributeAccessIssue], + [62, DiagnosticRule.reportAssignmentType], + [70, DiagnosticRule.reportAttributeAccessIssue], + [87, DiagnosticRule.reportAssignmentType], + [94, DiagnosticRule.reportAttributeAccessIssue], + [181, DiagnosticRule.reportAttributeAccessIssue], + [209, DiagnosticRule.reportAttributeAccessIssue], + [212, DiagnosticRule.reportAttributeAccessIssue], + [65, DiagnosticRule.reportArgumentType], + [91, DiagnosticRule.reportArgumentType], + ] + ); + const expectedAttributes = [ + ['nonexistent_member', 'Container[Any]'], + ['nonexistent_member', 'Container[Any]'], + ['nonexistent_member', 'Container[int]'], + ['nonexistent_member', 'list[int]'], + ['nonexistent_member', 'list[int]'], + ['nonexistent_member', 'Constructed[int]'], + ['upper', 'int'], + ['upper', 'int'], ]; - analysisResults[0].errors.forEach((diagnostic, index) => { - assert.strictEqual(diagnostic.getRule(), DiagnosticRule.reportAttributeAccessIssue); + const attributeErrors = analysisResults[0].errors.filter( + (diagnostic) => diagnostic.getRule() === DiagnosticRule.reportAttributeAccessIssue + ); + assert.strictEqual(attributeErrors.length, expectedAttributes.length); + attributeErrors.forEach((diagnostic, index) => { + const [attribute, className] = expectedAttributes[index]; assert.strictEqual( diagnostic.message, - `Cannot access attribute "nonexistent_member" for class "${expectedClasses[index]}"\n` + - '\u00a0\u00a0Attribute "nonexistent_member" is unknown' + `Cannot access attribute "${attribute}" for class "${className}"\n` + + `\u00a0\u00a0Attribute "${attribute}" is unknown` ); }); }); test('OverloadCall14', () => { const analysisResults = TestUtils.typeAnalyzeSampleFiles(['overloadCall14.py']); - TestUtils.validateResults(analysisResults, 6); + TestUtils.validateResults(analysisResults, 16); + // Preserve both invalid-call controls and the supported operations rejected by the rollback. + assert.deepStrictEqual( + analysisResults[0].errors.map((diagnostic) => [diagnostic.range.start.line + 1, diagnostic.getRule()]), + [ + [75, DiagnosticRule.reportArgumentType], + [77, DiagnosticRule.reportArgumentType], + [81, DiagnosticRule.reportArgumentType], + [83, DiagnosticRule.reportArgumentType], + [86, DiagnosticRule.reportArgumentType], + [89, DiagnosticRule.reportCallIssue], + [89, DiagnosticRule.reportArgumentType], + [93, DiagnosticRule.reportCallIssue], + [93, DiagnosticRule.reportArgumentType], + [96, DiagnosticRule.reportOperatorIssue], + [112, DiagnosticRule.reportArgumentType], + [112, DiagnosticRule.reportAttributeAccessIssue], + [115, DiagnosticRule.reportOperatorIssue], + [121, DiagnosticRule.reportAssignmentType], + [105, DiagnosticRule.reportArgumentType], + [108, DiagnosticRule.reportArgumentType], + ] + ); const attributeErrors = analysisResults[0].errors.filter( (diagnostic) => diagnostic.getRule() === DiagnosticRule.reportAttributeAccessIssue ); assert.strictEqual(attributeErrors.length, 1); assert.strictEqual( attributeErrors[0].message, - 'Cannot access attribute "nonexistent_member" for class "Container[int]"\n' + + 'Cannot access attribute "nonexistent_member" for class "Container[str]"\n' + '\u00a0\u00a0Attribute "nonexistent_member" is unknown' ); }); @@ -970,6 +1069,57 @@ test('Constructor33', () => { TestUtils.validateResults(analysisResults, 0); }); +test('Constructor34', () => { + const analysisResults = TestUtils.typeAnalyzeSampleFiles(['constructor34.py']); + + TestUtils.validateResults(analysisResults, 6); + assert.deepStrictEqual( + analysisResults[0].errors.map((diagnostic) => [ + diagnostic.range.start.line + 1, + diagnostic.getRule(), + diagnostic.message, + ]), + [ + [ + 25, + DiagnosticRule.reportAttributeAccessIssue, + 'Cannot assign to attribute "_data" for class "CacheControl*"\n' + + '\u00a0\u00a0Type "dict[tuple[str, str | None], _VT@dict] | dict[str, str | None] | dict[tuple[str, str | None], Unknown] | MutableMapping[str, str | None] | MutableMapping[tuple[str, str | None], Unknown]" is not assignable to type "MutableMapping[str, str | None]"\n' + + '\u00a0\u00a0\u00a0\u00a0"MutableMapping[tuple[str, str | None], Unknown]" is not assignable to "MutableMapping[str, str | None]"\n' + + '\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Type parameter "_KT@MutableMapping" is invariant, but "tuple[str, str | None]" is not the same as "str"', + ], + [ + 36, + DiagnosticRule.reportArgumentType, + 'Argument of type "Literal[1]" cannot be assigned to parameter "key" of type "str" in function "__setitem__"\n' + + '\u00a0\u00a0"Literal[1]" is not assignable to "str"', + ], + [ + 38, + DiagnosticRule.reportArgumentType, + 'Argument of type "Literal[1]" cannot be assigned to parameter "value" of type "str | None" in function "__setitem__"\n' + + '\u00a0\u00a0Type "Literal[1]" is not assignable to type "str | None"\n' + + '\u00a0\u00a0\u00a0\u00a0"Literal[1]" is not assignable to "str"\n' + + '\u00a0\u00a0\u00a0\u00a0"Literal[1]" is not assignable to "None"', + ], + [ + 40, + DiagnosticRule.reportAttributeAccessIssue, + 'Cannot access attribute "nonexistent_member" for class "dict[str, str | None]"\n' + + '\u00a0\u00a0Attribute "nonexistent_member" is unknown', + ], + [116, DiagnosticRule.reportCallIssue, 'No overloads for "__init__" match the provided arguments'], + [ + 116, + DiagnosticRule.reportArgumentType, + 'Argument of type "list[int] | set[str]" cannot be assigned to parameter "values" of type "set[int]" in function "__init__"\n' + + '\u00a0\u00a0Type "list[int] | set[str]" is not assignable to type "set[int]"\n' + + '\u00a0\u00a0\u00a0\u00a0"list[int]" is not assignable to "set[int]"', + ], + ] + ); +}); + test('ConstructorCallable1', () => { const analysisResults = TestUtils.typeAnalyzeSampleFiles(['constructorCallable1.py']); diff --git a/packages/pyright-internal/src/tests/typeUtils.test.ts b/packages/pyright-internal/src/tests/typeUtils.test.ts index 5e9885e77353..6af05857530d 100644 --- a/packages/pyright-internal/src/tests/typeUtils.test.ts +++ b/packages/pyright-internal/src/tests/typeUtils.test.ts @@ -31,21 +31,19 @@ import { import { Uri } from '../common/uri/uri'; import { ParamCategory } from '../parser/parseNodes'; -test('Map signatures preserves expanded overloads and their order', () => { +test('Map signatures preserves replaced overloads and their order', () => { const unknown = UnknownType.create(); const any = AnyType.create(); const first = createFunction(unknown, unknown, unknown); const second = createFunction(any, any, any); const implementation = createFunction(unknown, any, unknown); - const expandedFirst = createFunction(unknown, unknown, any); - const expandedSecond = createFunction(any, any, unknown); const original = OverloadedType.create([first, second], implementation); - const replacement = OverloadedType.create([expandedFirst, expandedSecond]); + const replacement = createFunction(unknown, unknown, any); const mapped = mapSignatures(original, (signature) => (signature === first ? replacement : signature)); assert.ok(mapped && isOverloaded(mapped)); - assert.deepStrictEqual(OverloadedType.getOverloads(mapped), [expandedFirst, expandedSecond, second]); + assert.deepStrictEqual(OverloadedType.getOverloads(mapped), [replacement, second]); assert.strictEqual(OverloadedType.getImplementation(mapped), implementation); assert.deepStrictEqual(OverloadedType.getOverloads(original), [first, second]); assert.strictEqual(