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

Higher-order function is not covered by crosshair cover #174

Closed
pschanely opened this issue Oct 7, 2022 · 1 comment
Closed

Higher-order function is not covered by crosshair cover #174

pschanely opened this issue Oct 7, 2022 · 1 comment

Comments

@pschanely
Copy link
Owner

pschanely commented Oct 7, 2022

Expected vs actual behavior
The TIM Improves Merging project found that crosshair could not generate two inputs to cover each branch of the following function (see additional discussion in the report PDF). Here, we see only the first branch is covered:

$ cat callableex.py
from typing import Any, Dict, List, Optional, Tuple, Generator, Callable, Iterable, IO, TypeVar, Mapping

def lambdaFn(a: Callable[[int], int]):
    if a:
        return a(2) + 4
    else:
        return "hello"

$ crosshair cover callableex.lambdaFn
lambdaFn(lambda *a: None)

One might expect to see inputs that cover both paths in the if statement.

@pschanely
Copy link
Owner Author

pschanely commented Oct 7, 2022

(I'm self-reporting and self-answering here, for the benefit of future CrossHair users)

In order to generate None CrossHair demands that the type have the Optional[] wrapper. This might be a common sort of stumbling block - mypy only recently made --no-implicit-optional the default behavior.

Adding Optional gives us the two paths we expect:

$ cat callableex.py
from typing import Any, Dict, List, Optional, Tuple, Generator, Callable, Iterable, IO, TypeVar, Mapping

def lambdaFn(a: Optional[Callable[[int], int]]):
    if a:
        return a(2) + 4
    else:
        return "hello"
$ crosshair cover callableex.lambdaFn
lambdaFn(lambda *a: None)
lambdaFn(None)

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

No branches or pull requests

1 participant