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 inference for multi-statement initialization #254

Closed
JukkaL opened this issue Jul 23, 2013 · 2 comments
Closed

Type inference for multi-statement initialization #254

JukkaL opened this issue Jul 23, 2013 · 2 comments
Labels

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 23, 2013

Improve type inference by making it possible to infer the type of a variable from multiple (conditional) assignments.

This should be valid:

x = None
if c:
    x = 'a'
# type of x is str

Another example:

if c:
    x = None
else:
    x = 'a'
# type of x is str

Later on, we could extend this to precise typing of None values using union types.

Edit: restricted this issue to multiple initialization.

@JukkaL JukkaL removed the front end label Jul 25, 2014
@JukkaL
Copy link
Collaborator Author

JukkaL commented Jul 25, 2014

We should not generally infer union types like this, as it would probably be too confusing. This would only be used for None and potentially in cases where there is a non-trivial common supertype, such as in cases like this:

class Animal: ...
class Dog(Animal): ...
class Cat(Animal): ...
if c:
    x = Dog()
else:
    x = Cat() 
# Type of x is Animal? Maybe even Union[Cat, Dog] since Cat and Dog are related, but we don't
# want to generate unions such as Union[int, str], I think, unless x is explicitly declared
# to have a union type.

@JukkaL JukkaL changed the title More general type inference Type inference for multi-statement initialization Jul 25, 2014
@JukkaL JukkaL added the priority label Dec 6, 2015
@JukkaL
Copy link
Collaborator Author

JukkaL commented Dec 31, 2015

#1099 covers the case of None initializer. Let's not infer union types or common supertypes yet.

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

1 participant