Improve typing support for the UNSET symbol.

Review Request #13191 — Created Aug. 7, 2023 and submitted

Information

Djblets
release-4.x

Reviewers

This reworks the internals of the UNSET symbol (which was introduced in
Djblets 3.3) and makes it easier to integrate it with typing logic and
type checkers.

UnsetSymbol is now an Enum, allowing UNSET to be defined as a
Literal. This works along with a new Unsettable[_T] type alias to
mark whether a parameter or variable allows UNSET to be set.

By making this a Literal, type checkers can type narrow when a code
compares a value to UNSET, avoiding any type errors that still wanted
to consider UnsetSymbol a possible value.

Made use of this in some in-progress code. Verified that mypy and pyright
successfully narrowed the types when comparing against UNSET.

Summary ID
Improve typing support for the UNSET symbol.
This reworks the internals of the UNSET symbol (which was introduced in Djblets 3.3) and makes it easier to integrate it with typing logic and type checkers. `UnsetSymbol` is now an `Enum`, allowing `UNSET` to be defined as a `Literal`. This works along with a new `Unsettable[_T]` type alias to mark whether a parameter or variable allows `UNSET` to be set. By making this a `Literal`, type checkers can type narrow when a code compares a value to `UNSET`, avoiding any type errors that still wanted to consider `UnsetSymbol` a possible value.
378e5a3ca00584065981c8c435d4b1e75bc3ecc0
Description From Last Updated

'enum.EnumMeta' imported but unused Column: 1 Error code: F401

reviewbotreviewbot

'typing.Any' imported but unused Column: 1 Error code: F401

reviewbotreviewbot
Checks run (1 failed, 1 succeeded)
flake8 failed.
JSHint passed.

flake8

chipx86
david
  1. Ship It!
  2. 
      
chipx86
Review request changed

Status: Closed (submitted)

Change Summary:

Pushed to master (0b0f0de)
Loading...