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

Type annotation cannot be unified with union types #427

Open
JSAbrahams opened this issue Jan 3, 2023 · 0 comments · Fixed by #430
Open

Type annotation cannot be unified with union types #427

JSAbrahams opened this issue Jan 3, 2023 · 0 comments · Fixed by #430
Assignees
Labels
bug: check Something in the type check module isn't working (as intended)

Comments

@JSAbrahams
Copy link
Owner

JSAbrahams commented Jan 3, 2023

Description of Bug

When we add certain type annotations, which are correct, the check stage complains.
We know they are correct, however, in the current implementation because the output does actually have these unions.

How to Reproduce

Give as input:

class MyClass
    def __str__() -> String => "M"

def a := if True then 20 else MyClass()

print(a)

Which gives output

from typing import Union

class MyClass:
    def __str__() -> str:
        return "M"

a: Union[int, MyClass] = 20 if True else MyClass()

print(a)

However, if we give input

class MyClass
    def __str__() -> String => "M"

def a: Union[Int, MyClass] := if True then 20 else MyClass()

print(a)

We get the following:

Error: Expected an Union[Int, MyClass], was an Int
 ──→ assign_with_if_different_types.mamba:4:47
   4 | def a: Union[String, MyClass] := if True then 20 else MyClass()
                                                     ^^
     └─→ variable and expression: `if`
       4 | def a: Union[String, MyClass] := if True then 20 else MyClass()

Expected behavior

The check stage should pass.
Perhaps this is a quick fix by flipping around the constraint in the variable definition generation stage/function.

Additional context

Putting in milestone 3.7.0 in case this is a non-trivial fix.
It not a critical error, but if it is an easy fix would be nice to have for 3.6.0

@JSAbrahams JSAbrahams added the bug: check Something in the type check module isn't working (as intended) label Jan 3, 2023
@JSAbrahams JSAbrahams added this to the v0.3.7 | Type alias milestone Jan 3, 2023
@JSAbrahams JSAbrahams self-assigned this Jan 3, 2023
@JSAbrahams JSAbrahams changed the title Correct type annotations seen as wrong Type annotation cannot be unified with union types Jan 8, 2023
@JSAbrahams JSAbrahams linked a pull request Jan 8, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug: check Something in the type check module isn't working (as intended)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant