[red-knot] don't include Unknown in the type for a conditionally-defined import (#13563)

## Summary

Fixes the bug described in #13514 where an unbound public type defaulted
to the type or `Unknown`, whereas it should only be the type if unbound.

## Test Plan

Added a new test case

---------

Co-authored-by: Carl Meyer <carl@astral.sh>
This commit is contained in:
aditya pillai
2024-10-16 16:46:03 -04:00
committed by GitHub
parent 2095ea8372
commit ed4a0b34ba
10 changed files with 126 additions and 91 deletions

View File

@@ -1,14 +1,12 @@
# Except star
TODO(Alex): Once we support `sys.version_info` branches, we can set `--target-version=py311` in these tests and the inferred type will just be `BaseExceptionGroup`
## Except\* with BaseException
```py
try:
x
except* BaseException as e:
reveal_type(e) # revealed: Unknown | BaseExceptionGroup
reveal_type(e) # revealed: BaseExceptionGroup
```
## Except\* with specific exception
@@ -18,7 +16,7 @@ try:
x
except* OSError as e:
# TODO(Alex): more precise would be `ExceptionGroup[OSError]`
reveal_type(e) # revealed: Unknown | BaseExceptionGroup
reveal_type(e) # revealed: BaseExceptionGroup
```
## Except\* with multiple exceptions
@@ -28,5 +26,5 @@ try:
x
except* (TypeError, AttributeError) as e:
#TODO(Alex): more precise would be `ExceptionGroup[TypeError | AttributeError]`.
reveal_type(e) # revealed: Unknown | BaseExceptionGroup
reveal_type(e) # revealed: BaseExceptionGroup
```