Repository navigation
Option/Either: Return both outcomes from conditional return types - #53
Merged
Merged
Conversation
`@psalm-param $mapFunc callable(T):U` has the name before the type, so Psalm never read it and U was never bound. mapSafely also said it returned self<T> when it returns the mapped Option<U>. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An Option<T> whose T isn't never could be some or none, but types like valueOr's (T is never ? TT : T) only described the some case. Older Psalm couldn't decide "T is never" and unioned both branches, which hid this. Psalm 7.3.1 decides it (vimeo/psalm#13433), so Option<int>->valueOr(null) became int and match() on an Either<L, R> returned only the right callback's type. Each conditional now ends in the case where either outcome is possible. For Either that also uses the other side being never: Either::left(1) is an Either<int, never>, so leftOr() on it is still just int. Fixed along the way: - orRight had its branches swapped. - mapRight, andThen and flatMapLeft returned the unmapped type when the other side wasn't never. - leftNotNull, rightNotNull, leftNotFalsy and rightNotFalsy dropped the value the Either keeps when it's already the other side. - filterLeft and filterRight dropped the new value. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Psalm 5 unions both branches of every conditional return type, so it can't check the types fixed in the last commit. Psalm 7 needs PHP 8.2+ while PHPUnit still runs on 7.4 to 8.1, so Psalm now runs in a separate job on PHP 8.4 from its phar, and vimeo/psalm leaves require-dev so the lock still installs on 7.4. What Psalm 7 reports beyond the types: - Unused code: off, since every public method is the library's API. - ClassMustBeFinal: suppressed for src, since callers may extend Option, Either and the exception. The test classes are now final. - ImpureMethodCall: MissingValueException's constructor only passed its arguments to Exception's, so it's gone. - The tests' assertEquals() on strings are now assertSame(), and SomeComplexThing::doWork() checks for null explicitly. - Baselined: 14 in tests that pass callbacks Psalm proves never run, or assert what Psalm already knows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
13 of these 15 checks fail on the types before the last two type fixes and pass after, on Psalm 7.4.0. The other two pin the cases where one outcome is ruled out: Option::none()->valueOr(null) is null and Either::left($int)->leftOr(null) is int. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖
Psalm 7.3.1 started deciding our conditional return types, and they were wrong.
Option<int>->valueOr(null)is now typedint, andmatch()on anEither<L, R>returns only the right callback's type. The iFixit app picked up 49 Psalm errors from this, and it carries a stub of the corrected types until a release has this fix (iFixit/ifixit#66586).Why it broke now
Types like
valueOr's(T is never ? TT : T)only describe the case where the Option holds a value. Older Psalm couldn't decideT is neverfor anOption<int>, so it unioned both branches and the result happened to be right. vimeo/psalm#13433 taught Psalm to decide it, so it now takes theTbranch alone.Fix
Every conditional now ends in the case where both outcomes are possible, e.g.
(T is never ? TT : T|TT). ForEitherit also uses the other side beingnever, soEither::left(1)->leftOr(null)is still justint.Reading every conditional turned up more wrong types:
orRighthad its branches swapped.mapRight,andThenandflatMapLeftreturned the unmapped type.*NotNull/*NotFalsyandfilterLeft/filterRighttypes dropped a value they can return.mapandmapSafelyhad@psalm-param $mapFunc callable(T):U, name first, so Psalm never boundU.mapSafelyalso said it returnedself<T>.CI
Psalm 5 can't check any of this, because it always unions both branches. Psalm 7 needs PHP 8.2+, so it moves to its own job on PHP 8.4 using the 7.4.0 phar.
vimeo/psalmleaves require-dev, so the lock still installs on 7.4 for PHPUnit. The commit message for that change lists how each new Psalm 7 report was handled. 14 test-only reports are baselined.tests/ReturnTypes.phpholds 15@psalm-check-type-exactchecks. 13 fail onmaster's types and all pass here.QA
Tests/Psalm/Stubs/php7-optional.phpstubwith no new Psalm errors🤖 Generated with Claude Code