Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@psalm-param array<string> does not work with @psalm-assert non-empty-list #3906

Closed
dFayet opened this issue Jul 28, 2020 · 6 comments
Closed
Labels

Comments

@dFayet
Copy link

dFayet commented Jul 28, 2020

This issue seems quite related with #2636

With the latest version of Psalm (3.12.2) the examples given in #2636 are working fine, but if I want to give stricter type to the the entry argument, for example list<string> I receive an error report:
ERROR: TypeDoesNotContainType - 20:13 - Cannot resolve types for $values - list<string> does not contain non-empty-list<mixed>
But not if I pass list<mixed> (same for array).

Example: https://psalm.dev/r/9d7436fe23

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/9d7436fe23
<?php
  class Assert {
    /**
     * @psalm-assert non-empty-list $array
     *
     * @param mixed  $array
     * @param string $message
     *
     * @throws InvalidArgumentException
     */
    public static function isNonEmptyList($array, $message = ''): void
    {
    } 
}
/**
 * @psalm-param list<string> $values
 */
function consume(array $values): void
{
    Assert::isNonEmptyList($values);
}
Psalm output (using commit 8fe1fbc):

ERROR: TypeDoesNotContainType - 20:13 - Cannot resolve types for $values - list<string> does not contain non-empty-list<mixed>

@weirdan weirdan added the bug label Jul 28, 2020
@greg0ire
Copy link
Contributor

This also works: https://psalm.dev/r/789f4fa5e6

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/789f4fa5e6
<?php
  class Assert {
    /**
     * @psalm-assert non-empty-list $array
     *
     * @param mixed  $array
     * @param string $message
     *
     * @throws InvalidArgumentException
     */
    public static function isNonEmptyList($array, $message = ''): void
    {
    } 
}
/**
 * @psalm-param non-empty-list<string> $values
 */
function consume(array $values): void
{
    Assert::isNonEmptyList($values);
}
Psalm output (using commit 8fe1fbc):

No issues!

@dFayet
Copy link
Author

dFayet commented Jul 28, 2020

@greg0ire yes, but what if $values is empty? https://psalm.dev/r/7b3899ba70

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/7b3899ba70
<?php
  class Assert {
    /**
     * @psalm-assert non-empty-list $array
     *
     * @param mixed  $array
     * @param string $message
     *
     * @throws InvalidArgumentException
     */
    public static function isNonEmptyList($array, $message = ''): void
    {
    } 
}
/**
 * @psalm-param non-empty-list<string> $values
 */
function consume(array $values): void
{
    Assert::isNonEmptyList($values);
}

consume([]);
Psalm output (using commit 8fe1fbc):

ERROR: InvalidArgument - 23:9 - Argument 1 of consume expects non-empty-list<string>, array<empty, empty> provided

@greg0ire
Copy link
Contributor

Then you get a rightful error message… if you assert the list is not empty inside consume(), then surely consume() itself is not supposed to be called with an empty list, right?

@muglug muglug closed this as completed in 7367e8e Jul 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants