Skip to content

Conversation

@sharkdp
Copy link
Contributor

@sharkdp sharkdp commented Nov 3, 2025

Summary

Allow values of type None in type expressions. The typing spec could be more explicit on whether this is actually allowed or not, but it seems relatively harmless and does help in some use cases like:

try:
    from module import MyClass
except ImportError:
    MyClass = None  # ty: ignore


def f(m: MyClass):
    pass

Test Plan

Updated tests, ecosystem check.

@sharkdp sharkdp added the ty Multi-file analysis & type inference label Nov 3, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Nov 3, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@github-actions
Copy link
Contributor

github-actions bot commented Nov 3, 2025

mypy_primer results

Changes were detected when running on open source projects
kopf (https://github.com/nolar/kopf)
- kopf/_cogs/helpers/thirdparty.py:41:36: error[invalid-type-form] Variable of type `None` is not allowed in a type expression
- kopf/_cogs/helpers/thirdparty.py:45:36: error[invalid-type-form] Variable of type `None` is not allowed in a type expression
- Found 47 diagnostics
+ Found 45 diagnostics

mypy (https://github.com/python/mypy)
- mypy/typeshed/stdlib/tty.pyi:25:43: error[invalid-type-form] Variable of type `None` is not allowed in a type expression
- mypy/typeshed/stdlib/tty.pyi:26:46: error[invalid-type-form] Variable of type `None` is not allowed in a type expression
- Found 1839 diagnostics
+ Found 1837 diagnostics

prefect (https://github.com/PrefectHQ/prefect)
+ src/integrations/prefect-dbt/prefect_dbt/cli/credentials.py:166:42: warning[possibly-missing-attribute] Attribute `get_configs` may be missing on object of type `Unknown | None`
- src/integrations/prefect-dbt/prefect_dbt/cli/credentials.py:133:23: error[invalid-type-form] Variable of type `None` is not allowed in a type expression
- src/integrations/prefect-dbt/prefect_dbt/cli/credentials.py:134:23: error[invalid-type-form] Variable of type `None` is not allowed in a type expression
- src/integrations/prefect-dbt/prefect_dbt/cli/credentials.py:135:23: error[invalid-type-form] Variable of type `None` is not allowed in a type expression
- Found 3308 diagnostics
+ Found 3306 diagnostics

aiohttp (https://github.com/aio-libs/aiohttp)
- aiohttp/client_exceptions.py:161:28: error[invalid-type-form] Variable of type `None` is not allowed in a type expression
- Found 161 diagnostics
+ Found 160 diagnostics

dd-trace-py (https://github.com/DataDog/dd-trace-py)
+ ddtrace/internal/ci_visibility/coverage.py:105:12: warning[possibly-missing-attribute] Attribute `_collector` may be missing on object of type `Unknown | None`
+ ddtrace/internal/ci_visibility/coverage.py:105:44: warning[possibly-missing-attribute] Attribute `_collector` may be missing on object of type `Unknown | None`
+ ddtrace/internal/ci_visibility/coverage.py:127:9: warning[possibly-missing-attribute] Attribute `switch_context` may be missing on object of type `Unknown | None`
- ddtrace/internal/ci_visibility/coverage.py:104:45: error[invalid-type-form] Variable of type `None` is not allowed in a type expression
- ddtrace/internal/ci_visibility/coverage.py:113:20: error[invalid-type-form] Variable of type `None` is not allowed in a type expression
- ddtrace/internal/ci_visibility/coverage.py:125:44: warning[unused-ignore-comment] Unused blanket `type: ignore` directive
- ddtrace/internal/ci_visibility/coverage.py:134:20: error[invalid-type-form] Variable of type `None` is not allowed in a type expression
- ddtrace/internal/ci_visibility/coverage.py:163:44: warning[unused-ignore-comment] Unused blanket `type: ignore` directive
- ddtrace/internal/ci_visibility/coverage.py:178:22: error[invalid-type-form] Variable of type `None` is not allowed in a type expression
+ ddtrace/internal/ci_visibility/coverage.py:179:12: warning[possibly-missing-attribute] Attribute `_collector` may be missing on object of type `Unknown | None`
+ ddtrace/internal/ci_visibility/coverage.py:179:39: warning[possibly-missing-attribute] Attribute `_collector` may be missing on object of type `Unknown | None`
- ddtrace/internal/ci_visibility/coverage.py:188:29: error[invalid-type-form] Variable of type `None` is not allowed in a type expression
+ ddtrace/internal/ci_visibility/coverage.py:184:26: warning[possibly-missing-attribute] Attribute `_collector` may be missing on object of type `Unknown | None`
- Found 8156 diagnostics
+ Found 8155 diagnostics
No memory usage changes detected ✅

@sharkdp sharkdp changed the title [ty] Allow values of type None in type expressions [ty] Allow values of type None in type expressions Nov 3, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Nov 3, 2025

ecosystem-analyzer results

Lint rule Added Removed Changed
invalid-type-form 0 13 0
possibly-missing-attribute 7 0 0
unused-ignore-comment 0 2 0
Total 7 15 0

Full report with detailed diff (timing results)

@sharkdp sharkdp marked this pull request as ready for review November 4, 2025 13:50
@sharkdp sharkdp merged commit 2e7ab00 into main Nov 4, 2025
43 checks passed
@sharkdp sharkdp deleted the david/allow-none-in-type-expressions branch November 4, 2025 15:29
carljm added a commit to MatthewMckee4/ruff that referenced this pull request Nov 6, 2025
* main: (188 commits)
  [ty] Discover site-packages from the environment that ty is installed in (astral-sh#21286)
  [ty] Make special cases for `UnionType` slightly narrower (astral-sh#21276)
  Require ignore 0.4.24 in `Cargo.toml` (astral-sh#21292)
  [ty] Favour imported symbols over builtin symbols (astral-sh#21285)
  docs: revise Ruff setup instructions for Zed editor (astral-sh#20935)
  [ty] Update salsa (astral-sh#21281)
  [syntax-error]: no binding for nonlocal  PLE0117 as a semantic syntax error (astral-sh#21032)
  [ty] Constraining a typevar with itself (possibly via union or intersection) (astral-sh#21273)
  [`ruff`] Fix false positives on starred arguments (`RUF057`) (astral-sh#21256)
  [ty] Simplify unions containing multiple type variables during inference (astral-sh#21275)
  [ty] Add `ty_server::Db` trait (astral-sh#21241)
  [ty] Refactor `Range` to/from `TextRange` conversion as prep for notebook support (astral-sh#21230)
  [ty] Fix playground crash when file name includes path separator (astral-sh#21151)
  [`refurb`] Fix false negative for underscores before sign in `Decimal` constructor (`FURB157`) (astral-sh#21190)
  [ty] Allow values of type `None` in type expressions (astral-sh#21263)
  Run codspeed benchmarks with `profiling` profile (astral-sh#21261)
  [ty] Update expected diagnostic count in benchmarks (astral-sh#21269)
  Avoid extra parentheses for long `match` patterns with `as` captures (astral-sh#21176)
  [ty] Update salsa (astral-sh#21265)
  [ty] `dict` is not assignable to `TypedDict` (astral-sh#21238)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ecosystem-analyzer ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants