Skip to content

Fix: check datasets of providers without a @return type - #275

Merged
alies-dev merged 1 commit into
psalm:masterfrom
alies-dev:fix/269-untyped-providers
Nov 10, 2026
Merged

alies-dev merged 1 commit into
psalm:masterfrom
alies-dev:fix/269-untyped-providers

Conversation

@alies-dev

Copy link
Copy Markdown
Contributor

Providers with only a native return type (iterable, array, Generator) get MixedReturnStatement, and their datasets are not checked.

Before

<?php
final class MyTestCase extends TestCase
{
    /** @psalm-pure */
    public static function provide(): iterable { // MixedReturnStatement: Providers must return iterable<array-key, array<array-key, mixed>>, possibly different iterable[pure]<mixed, mixed> provided
        yield 'one' => [1];
    }

    #[DataProvider('provide')]
    public function testSomething(int $i): void {
        $this->assertSame(1, $i);
    }
}
<?php
final class MyTestCase extends TestCase
{
    /** @psalm-pure */
    public static function provide(): array { // MixedReturnStatement: Providers must return iterable<array-key, array<array-key, mixed>>, possibly different array<array-key, mixed> provided
        return ['one' => ['x']];
    }

    #[DataProvider('provide')]
    public function testSomething(int $i): void { // no issue
        $this->assertSame(1, $i);
    }
}

After

    public static function provide(): iterable { // no issue
    public static function provide(): array { // no issue
    public function testSomething(int $i): void { // InvalidArgument: Argument 1 of NS\MyTestCase::testSomething expects int, 'x' provided by NS\MyTestCase::provide():(array{one: list{'x'}})

Datasets are checked against the type Psalm infers from the provider's body, one dataset at a time, for providers declared in the test class or in a trait it uses. Providers in a parent or external class, and providers using yield from, keep MixedReturnStatement, which now ends with ; add a @return type to NS\BaseTest::provide().

Fixes #269

A provider with only a native return type was reported as MixedReturnStatement and its datasets went unchecked. They are now checked against the type Psalm infers from the body of a provider declared in the test case or in a trait it uses. Other providers (parent or external class, yield from, non-iterable inferred type) keep the MixedReturnStatement, which now asks for a @return type.
@alies-dev alies-dev self-assigned this Nov 10, 2026
@alies-dev
alies-dev marked this pull request as ready for review November 10, 2026 12:35
@alies-dev
alies-dev merged commit 51820fd into psalm:master Nov 10, 2026
8 checks passed
@alies-dev
alies-dev deleted the fix/269-untyped-providers branch November 10, 2026 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Providers without a @return type get MixedReturnStatement and their datasets go unchecked

1 participant