Skip to content

Comments

[ty] Support empty function bodies in if TYPE_CHECKING blocks#19372

Merged
carljm merged 12 commits intoastral-sh:mainfrom
MatthewMckee4:in-type-checking
Jul 16, 2025
Merged

[ty] Support empty function bodies in if TYPE_CHECKING blocks#19372
carljm merged 12 commits intoastral-sh:mainfrom
MatthewMckee4:in-type-checking

Conversation

@MatthewMckee4
Copy link
Contributor

@MatthewMckee4 MatthewMckee4 commented Jul 15, 2025

Summary

Resolves astral-sh/ty#339

Supports having a blank function body inside if TYPE_CHECKING block or in the elif or else of a if not TYPE_CHECKING block.

if TYPE_CHECKING:
    def foo() -> int: ...

if not TYPE_CHECKING: ...
else:     
    def bar() -> int: ...

Test Plan

Update function/return_type.md

@github-actions
Copy link
Contributor

github-actions bot commented Jul 15, 2025

mypy_primer results

Changes were detected when running on open source projects
pytest-robotframework (https://github.com/detachhead/pytest-robotframework)
+ warning[unused-ignore-comment] tests/conftest.py:93:76: Unused `ty: ignore` directive: 'invalid-return-type'
+ warning[unused-ignore-comment] tests/conftest.py:96:80: Unused `ty: ignore` directive: 'invalid-return-type'
+ warning[unused-ignore-comment] tests/conftest.py:99:55: Unused `ty: ignore` directive: 'invalid-return-type'
+ warning[unused-ignore-comment] tests/conftest.py:102:65: Unused `ty: ignore` directive: 'invalid-return-type'
+ warning[unused-ignore-comment] tests/conftest.py:105:77: Unused `ty: ignore` directive: 'invalid-return-type'
+ warning[unused-ignore-comment] tests/conftest.py:108:89: Unused `ty: ignore` directive: 'invalid-return-type'
+ warning[unused-ignore-comment] tests/conftest.py:111:99: Unused `ty: ignore` directive: 'invalid-return-type'
+ warning[unused-ignore-comment] tests/conftest.py:116:26: Unused `ty: ignore` directive: 'invalid-return-type'
+ warning[unused-ignore-comment] tests/conftest.py:203:82: Unused `ty: ignore` directive: 'invalid-return-type'
+ warning[unused-ignore-comment] tests/conftest.py:207:73: Unused `ty: ignore` directive: 'invalid-return-type'
+ warning[unused-ignore-comment] tests/conftest.py:214:47: Unused `ty: ignore` directive: 'invalid-return-type'
- Found 179 diagnostics
+ Found 190 diagnostics

kornia (https://github.com/kornia/kornia)
- error[invalid-return-type] kornia/utils/_compat.py:50:82: Function always implicitly returns `None`, which is not assignable to return type `tuple[Unknown, ...]`
- Found 790 diagnostics
+ Found 789 diagnostics

operator (https://github.com/canonical/operator)
- error[invalid-return-type] ops/charm.py:1437:25: Function always implicitly returns `None`, which is not assignable to return type `CharmEvents`
- error[invalid-return-type] ops/framework.py:394:25: Function always implicitly returns `None`, which is not assignable to return type `ObjectEvents`
- error[invalid-return-type] ops/framework.py:606:25: Function always implicitly returns `None`, which is not assignable to return type `FrameworkEvents`
- error[invalid-return-type] ops/framework.py:1158:28: Function always implicitly returns `None`, which is not assignable to return type `StoredStateData`
- error[invalid-return-type] ops/framework.py:1161:33: Function always implicitly returns `None`, which is not assignable to return type `str`
- Found 114 diagnostics
+ Found 109 diagnostics

comtypes (https://github.com/enthought/comtypes)
- error[invalid-return-type] comtypes/_post_coinit/misc.py:58:33: Function always implicitly returns `None`, which is not assignable to return type `GUID`
- error[invalid-return-type] comtypes/automation.py:816:39: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] comtypes/connectionpoints.py:35:43: Function always implicitly returns `None`, which is not assignable to return type `IEnumConnectionPoints`
- error[invalid-return-type] comtypes/connectionpoints.py:36:56: Function always implicitly returns `None`, which is not assignable to return type `IConnectionPoint`
- error[invalid-return-type] comtypes/connectionpoints.py:45:50: Function always implicitly returns `None`, which is not assignable to return type `IConnectionPointContainer`
- error[invalid-return-type] comtypes/connectionpoints.py:46:49: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] comtypes/connectionpoints.py:48:38: Function always implicitly returns `None`, which is not assignable to return type `IEnumConnections`
- error[invalid-return-type] comtypes/connectionpoints.py:57:46: Function always implicitly returns `None`, which is not assignable to return type `tuple[tagCONNECTDATA, int]`
- error[invalid-return-type] comtypes/connectionpoints.py:60:28: Function always implicitly returns `None`, which is not assignable to return type `IEnumConnections`
- error[invalid-return-type] comtypes/connectionpoints.py:78:46: Function always implicitly returns `None`, which is not assignable to return type `tuple[IConnectionPoint, int]`
- error[invalid-return-type] comtypes/connectionpoints.py:81:28: Function always implicitly returns `None`, which is not assignable to return type `IEnumConnectionPoints`
- error[invalid-return-type] comtypes/errorinfo.py:51:30: Function always implicitly returns `None`, which is not assignable to return type `GUID`
- error[invalid-return-type] comtypes/errorinfo.py:52:32: Function always implicitly returns `None`, which is not assignable to return type `str`
- error[invalid-return-type] comtypes/errorinfo.py:53:37: Function always implicitly returns `None`, which is not assignable to return type `str`
- error[invalid-return-type] comtypes/errorinfo.py:54:34: Function always implicitly returns `None`, which is not assignable to return type `str`
- error[invalid-return-type] comtypes/errorinfo.py:55:37: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] comtypes/persist.py:258:33: Function always implicitly returns `None`, which is not assignable to return type `str`
- error[invalid-return-type] comtypes/shelllink.py:143:29: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] comtypes/shelllink.py:147:30: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] comtypes/shelllink.py:278:29: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] comtypes/shelllink.py:282:30: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] comtypes/stream.py:57:71: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] comtypes/typeinfo.py:239:39: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] comtypes/typeinfo.py:243:46: Function always implicitly returns `None`, which is not assignable to return type `ITypeInfo`
- error[invalid-return-type] comtypes/typeinfo.py:247:50: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] comtypes/typeinfo.py:251:52: Function always implicitly returns `None`, which is not assignable to return type `ITypeInfo`
- error[invalid-return-type] comtypes/typeinfo.py:255:34: Function always implicitly returns `None`, which is not assignable to return type `ITypeComp`
- error[invalid-return-type] comtypes/typeinfo.py:261:14: Function always implicitly returns `None`, which is not assignable to return type `tuple[str, str | None, int, str | None]`
- error[invalid-return-type] comtypes/typeinfo.py:265:66: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] comtypes/typeinfo.py:325:34: Function always implicitly returns `None`, which is not assignable to return type `ITypeComp`
- error[invalid-return-type] comtypes/typeinfo.py:329:55: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] comtypes/typeinfo.py:333:51: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] comtypes/typeinfo.py:346:14: Function always implicitly returns `None`, which is not assignable to return type `tuple[str | None, str | None, int]`
- error[invalid-return-type] comtypes/typeinfo.py:352:48: Function always implicitly returns `None`, which is not assignable to return type `ITypeInfo`
- error[invalid-return-type] comtypes/typeinfo.py:360:43: Function always implicitly returns `None`, which is not assignable to return type `tuple[ITypeLib, int]`
- error[invalid-return-type] comtypes/typeinfo.py:364:71: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] comtypes/typeinfo.py:368:71: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] comtypes/typeinfo.py:372:68: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] comtypes/typeinfo.py:523:30: Function always implicitly returns `None`, which is not assignable to return type `GUID`
- error[invalid-return-type] comtypes/typeinfo.py:524:30: Function always implicitly returns `None`, which is not assignable to return type `str`
- error[invalid-return-type] comtypes/typeinfo.py:525:30: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] comtypes/typeinfo.py:526:34: Function always implicitly returns `None`, which is not assignable to return type `ITypeInfo`
- error[invalid-return-type] comtypes/typeinfo.py:531:59: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] comtypes/typeinfo.py:533:67: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] comtypes/typeinfo.py:1170:35: Function always implicitly returns `None`, which is not assignable to return type `ITypeInfo`
- error[invalid-return-type] comtypes/typeinfo.py:1191:47: Function always implicitly returns `None`, which is not assignable to return type `GUID`
- Found 466 diagnostics
+ Found 420 diagnostics

trio (https://github.com/python-trio/trio)
- error[invalid-return-type] src/trio/_core/_exceptions.py:120:14: Function always implicitly returns `None`, which is not assignable to return type `Self`
- error[invalid-return-type] src/trio/_file_io.py:304:57: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] src/trio/_file_io.py:306:61: Function always implicitly returns `None`, which is not assignable to return type `str`
- error[invalid-return-type] src/trio/_file_io.py:310:64: Function always implicitly returns `None`, which is not assignable to return type `T`
- error[invalid-return-type] src/trio/_file_io.py:312:57: Function always implicitly returns `None`, which is not assignable to return type `BinaryIO`
- error[invalid-return-type] src/trio/_file_io.py:314:51: Function always implicitly returns `None`, which is not assignable to return type `RawIOBase`
- error[invalid-return-type] src/trio/_file_io.py:316:72: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] src/trio/_file_io.py:318:59: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] src/trio/_file_io.py:320:53: Function always implicitly returns `None`, which is not assignable to return type `str`
- error[invalid-return-type] src/trio/_file_io.py:322:53: Function always implicitly returns `None`, which is not assignable to return type `str`
- error[invalid-return-type] src/trio/_file_io.py:324:57: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] src/trio/_file_io.py:325:57: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] src/trio/_file_io.py:326:61: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] src/trio/_file_io.py:327:61: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] src/trio/_file_io.py:328:61: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] src/trio/_file_io.py:329:69: Function always implicitly returns `None`, which is not assignable to return type `AnyStr`
- error[invalid-return-type] src/trio/_file_io.py:330:63: Function always implicitly returns `None`, which is not assignable to return type `memoryview[Unknown]`
- error[invalid-return-type] src/trio/_file_io.py:332:93: Function always implicitly returns `None`, which is not assignable to return type `AnyStr`
- error[invalid-return-type] src/trio/_file_io.py:333:87: Function always implicitly returns `None`, which is not assignable to return type `bytes`
- error[invalid-return-type] src/trio/_file_io.py:334:73: Function always implicitly returns `None`, which is not assignable to return type `AnyStr`
- error[invalid-return-type] src/trio/_file_io.py:336:94: Function always implicitly returns `None`, which is not assignable to return type `AnyStr`
- error[invalid-return-type] src/trio/_file_io.py:337:77: Function always implicitly returns `None`, which is not assignable to return type `list[AnyStr]`
- error[invalid-return-type] src/trio/_file_io.py:338:92: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] src/trio/_file_io.py:339:59: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] src/trio/_file_io.py:340:95: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] src/trio/_file_io.py:341:76: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] src/trio/_file_io.py:343:88: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] src/trio/_file_io.py:344:85: Function always implicitly returns `None`, which is not assignable to return type `AnyStr`
- error[invalid-return-type] src/trio/_socket.py:1120:59: Function always implicitly returns `None`, which is not assignable to return type `Awaitable[bytes]`
- error[invalid-return-type] src/trio/_socket.py:1139:14: Function always implicitly returns `None`, which is not assignable to return type `Awaitable[int]`
- error[invalid-return-type] src/trio/_socket.py:1157:14: Function always implicitly returns `None`, which is not assignable to return type `Awaitable[tuple[bytes, @Todo(Support for `typing.TypeAlias`)]]`
- error[invalid-return-type] src/trio/_socket.py:1176:14: Function always implicitly returns `None`, which is not assignable to return type `Awaitable[tuple[int, @Todo(Support for `typing.TypeAlias`)]]`
- error[invalid-return-type] src/trio/_socket.py:1198:18: Function always implicitly returns `None`, which is not assignable to return type `Awaitable[tuple[bytes, list[tuple[int, int, bytes]], int, object]]`
- error[invalid-return-type] src/trio/_socket.py:1221:18: Function always implicitly returns `None`, which is not assignable to return type `Awaitable[tuple[int, list[tuple[int, int, bytes]], int, object]]`
- error[invalid-return-type] src/trio/_socket.py:1235:61: Function always implicitly returns `None`, which is not assignable to return type `Awaitable[int]`
- error[invalid-return-type] src/trio/_subprocess.py:51:44: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] src/trio/_subprocess.py:829:14: Function always implicitly returns `None`, which is not assignable to return type `Process`
- error[invalid-return-type] src/trio/_subprocess.py:892:14: Function always implicitly returns `None`, which is not assignable to return type `CompletedProcess[bytes]`
- error[invalid-return-type] src/trio/_util.py:355:10: Function always implicitly returns `None`, which is not assignable to return type `(Fn, /) -> Fn`
- Found 785 diagnostics
+ Found 746 diagnostics

mongo-python-driver (https://github.com/mongodb/mongo-python-driver)
- error[invalid-return-type] bson/codec_options.py:266:14: Function always implicitly returns `None`, which is not assignable to return type `CodecOptions[_DocumentType]`
- error[invalid-return-type] bson/codec_options.py:270:50: Function always implicitly returns `None`, which is not assignable to return type `CodecOptions[Any]`
- error[invalid-return-type] bson/codec_options.py:273:38: Function always implicitly returns `None`, which is not assignable to return type `str`
- error[invalid-return-type] bson/codec_options.py:276:36: Function always implicitly returns `None`, which is not assignable to return type `dict[Any, Any]`
- error[invalid-return-type] bson/codec_options.py:281:47: Function always implicitly returns `None`, which is not assignable to return type `CodecOptions[_DocumentType]`
- error[invalid-return-type] bson/codec_options.py:284:30: Function always implicitly returns `None`, which is not assignable to return type `dict[str, Any]`
- error[invalid-return-type] bson/codec_options.py:287:46: Function always implicitly returns `None`, which is not assignable to return type `CodecOptions[_DocumentType]`
- Found 430 diagnostics
+ Found 423 diagnostics

pydantic (https://github.com/pydantic/pydantic)
- error[invalid-return-type] pydantic/v1/dataclasses.py:88:62: Function always implicitly returns `None`, which is not assignable to return type `DataclassT`
- Found 748 diagnostics
+ Found 747 diagnostics

pytest (https://github.com/pytest-dev/pytest)
- error[invalid-return-type] src/_pytest/_py/path.py:207:27: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] src/_pytest/_py/path.py:210:28: Function always implicitly returns `None`, which is not assignable to return type `int | float`
- error[invalid-return-type] src/_pytest/mark/structures.py:491:14: Function always implicitly returns `None`, which is not assignable to return type `MarkDecorator`
- error[invalid-return-type] src/_pytest/mark/structures.py:522:14: Function always implicitly returns `None`, which is not assignable to return type `MarkDecorator`
+ warning[unused-ignore-comment] src/_pytest/mark/structures.py:525:63: Unused blanket `type: ignore` directive
+ warning[unused-ignore-comment] src/_pytest/mark/structures.py:529:62: Unused blanket `type: ignore` directive
- Found 519 diagnostics
+ Found 517 diagnostics

bokeh (https://github.com/bokeh/bokeh)
- error[invalid-return-type] src/bokeh/core/has_props.py:49:39: Function always implicitly returns `None`, which is not assignable to return type `(F, /) -> F`
- Found 849 diagnostics
+ Found 848 diagnostics

prefect (https://github.com/PrefectHQ/prefect)
- error[invalid-return-type] src/prefect/_internal/concurrency/calls.py:158:35: Function always implicitly returns `None`, which is not assignable to return type `T`
- Found 3733 diagnostics
+ Found 3732 diagnostics

zulip (https://github.com/zulip/zulip)
- error[invalid-return-type] zerver/lib/partial.py:36:62: Function always implicitly returns `None`, which is not assignable to return type `(...) -> R`
- Found 7259 diagnostics
+ Found 7258 diagnostics

sympy (https://github.com/sympy/sympy)
- error[invalid-return-type] sympy/algebras/quaternion.py:122:27: Function always implicitly returns `None`, which is not assignable to return type `tuple[Expr, Expr, Expr, Expr]`
- error[invalid-return-type] sympy/core/add.py:194:27: Function always implicitly returns `None`, which is not assignable to return type `tuple[Expr, ...]`
+ warning[unused-ignore-comment] sympy/core/add.py:190:79: Unused blanket `type: ignore` directive
- error[invalid-return-type] sympy/core/expr.py:74:43: Function always implicitly returns `None`, which is not assignable to return type `Self`
- error[invalid-return-type] sympy/core/expr.py:91:73: Function always implicitly returns `None`, which is not assignable to return type `Basic`
- error[invalid-return-type] sympy/core/expr.py:94:41: Function always implicitly returns `None`, which is not assignable to return type `Expr`
- error[invalid-return-type] sympy/core/expr.py:99:70: Function always implicitly returns `None`, which is not assignable to return type `Expr`
- error[invalid-return-type] sympy/core/mul.py:181:27: Function always implicitly returns `None`, which is not assignable to return type `tuple[Expr, ...]`
+ warning[unused-ignore-comment] sympy/core/mul.py:177:79: Unused blanket `type: ignore` directive
- error[invalid-return-type] sympy/core/power.py:118:27: Function always implicitly returns `None`, which is not assignable to return type `tuple[Expr, Expr]`
- error[invalid-return-type] sympy/core/relational.py:847:27: Function always implicitly returns `None`, which is not assignable to return type `tuple[Expr, Expr]`
- error[invalid-return-type] sympy/core/relational.py:851:26: Function always implicitly returns `None`, which is not assignable to return type `Expr`
- error[invalid-return-type] sympy/core/relational.py:855:26: Function always implicitly returns `None`, which is not assignable to return type `Expr`
- error[invalid-return-type] sympy/external/gmpy.py:206:30: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] sympy/external/gmpy.py:207:32: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] sympy/external/gmpy.py:209:30: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:210:30: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:211:51: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:212:46: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:213:51: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:214:46: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:215:51: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:216:46: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:217:45: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:218:46: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:219:56: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:220:51: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:221:51: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:222:46: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:223:54: Function always implicitly returns `None`, which is not assignable to return type `tuple[MPZ, MPZ]`
- error[invalid-return-type] sympy/external/gmpy.py:224:49: Function always implicitly returns `None`, which is not assignable to return type `tuple[MPZ, MPZ]`
- error[invalid-return-type] sympy/external/gmpy.py:226:48: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:227:49: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:228:48: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:229:49: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:231:51: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:232:46: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:233:50: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:234:45: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:235:51: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:236:46: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:237:33: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:239:50: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] sympy/external/gmpy.py:240:50: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] sympy/external/gmpy.py:241:50: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] sympy/external/gmpy.py:242:50: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] sympy/external/gmpy.py:252:30: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] sympy/external/gmpy.py:255:32: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:257:34: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:259:30: Function always implicitly returns `None`, which is not assignable to return type `MPQ`
- error[invalid-return-type] sympy/external/gmpy.py:260:30: Function always implicitly returns `None`, which is not assignable to return type `MPQ`
- error[invalid-return-type] sympy/external/gmpy.py:261:57: Function always implicitly returns `None`, which is not assignable to return type `MPQ`
- error[invalid-return-type] sympy/external/gmpy.py:262:52: Function always implicitly returns `None`, which is not assignable to return type `MPQ`
- error[invalid-return-type] sympy/external/gmpy.py:263:57: Function always implicitly returns `None`, which is not assignable to return type `MPQ`
- error[invalid-return-type] sympy/external/gmpy.py:264:52: Function always implicitly returns `None`, which is not assignable to return type `MPQ`
- error[invalid-return-type] sympy/external/gmpy.py:265:57: Function always implicitly returns `None`, which is not assignable to return type `MPQ`
- error[invalid-return-type] sympy/external/gmpy.py:266:52: Function always implicitly returns `None`, which is not assignable to return type `MPQ`
- error[invalid-return-type] sympy/external/gmpy.py:267:45: Function always implicitly returns `None`, which is not assignable to return type `MPQ`
- error[invalid-return-type] sympy/external/gmpy.py:269:61: Function always implicitly returns `None`, which is not assignable to return type `MPQ`
- error[invalid-return-type] sympy/external/gmpy.py:270:56: Function always implicitly returns `None`, which is not assignable to return type `MPQ`
- error[invalid-return-type] sympy/external/gmpy.py:272:62: Function always implicitly returns `None`, which is not assignable to return type `MPQ`
- error[invalid-return-type] sympy/external/gmpy.py:273:57: Function always implicitly returns `None`, which is not assignable to return type `MPQ`
- error[invalid-return-type] sympy/external/gmpy.py:274:57: Function always implicitly returns `None`, which is not assignable to return type `MPQ`
- error[invalid-return-type] sympy/external/gmpy.py:275:52: Function always implicitly returns `None`, which is not assignable to return type `MPQ`
- error[invalid-return-type] sympy/external/gmpy.py:276:60: Function always implicitly returns `None`, which is not assignable to return type `tuple[MPQ, MPQ]`
- error[invalid-return-type] sympy/external/gmpy.py:277:55: Function always implicitly returns `None`, which is not assignable to return type `tuple[MPQ, MPQ]`
- error[invalid-return-type] sympy/external/gmpy.py:279:56: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] sympy/external/gmpy.py:280:56: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] sympy/external/gmpy.py:281:56: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] sympy/external/gmpy.py:282:56: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] sympy/external/gmpy.py:294:36: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] sympy/external/gmpy.py:295:36: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] sympy/external/gmpy.py:296:36: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:297:31: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:298:34: Function always implicitly returns `None`, which is not assignable to return type `tuple[MPZ, MPZ]`
- error[invalid-return-type] sympy/external/gmpy.py:300:34: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:301:34: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:302:47: Function always implicitly returns `None`, which is not assignable to return type `tuple[MPZ, MPZ, MPZ]`
- error[invalid-return-type] sympy/external/gmpy.py:304:47: Function always implicitly returns `None`, which is not assignable to return type `MPZ`
- error[invalid-return-type] sympy/external/gmpy.py:305:47: Function always implicitly returns `None`, which is not assignable to return type `tuple[MPZ, MPZ]`
- error[invalid-return-type] sympy/external/gmpy.py:306:40: Function always implicitly returns `None`, which is not assignable to return type `tuple[MPZ, bool]`
- error[invalid-return-type] sympy/external/gmpy.py:308:47: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] sympy/external/gmpy.py:309:49: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] sympy/external/gmpy.py:310:50: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] sympy/external/gmpy.py:312:36: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] sympy/external/gmpy.py:313:40: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] sympy/external/gmpy.py:314:39: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] sympy/external/gmpy.py:315:40: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] sympy/external/gmpy.py:316:43: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] sympy/external/gmpy.py:317:39: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] sympy/external/gmpy.py:318:43: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] sympy/external/gmpy.py:319:46: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] sympy/external/gmpy.py:320:50: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] sympy/external/gmpy.py:321:38: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] sympy/external/gmpy.py:322:45: Function always implicitly returns `None`, which is not assignable to return type `bool`
- error[invalid-return-type] sympy/functions/elementary/complexes.py:84:26: Function always implicitly returns `None`, which is not assignable to return type `MatrixBase | Expr`
+ warning[unused-ignore-comment] sympy/functions/elementary/complexes.py:888:52: Unused blanket `type: ignore` directive
- error[invalid-return-type] sympy/logic/boolalg.py:79:53: Function always implicitly returns `None`, which is not assignable to return type `Boolean`
+ warning[unused-ignore-comment] sympy/logic/boolalg.py:607:91: Unused blanket `type: ignore` directive
+ warning[unused-ignore-comment] sympy/logic/boolalg.py:778:91: Unused blanket `type: ignore` directive
- error[invalid-return-type] sympy/logic/boolalg.py:96:73: Function always implicitly returns `None`, which is not assignable to return type `Basic`
- error[invalid-return-type] sympy/logic/boolalg.py:99:41: Function always implicitly returns `None`, which is not assignable to return type `Boolean`
- error[invalid-return-type] sympy/logic/boolalg.py:611:27: Function always implicitly returns `None`, which is not assignable to return type `tuple[Boolean, ...]`
- error[invalid-return-type] sympy/logic/boolalg.py:782:27: Function always implicitly returns `None`, which is not assignable to return type `tuple[Boolean, ...]`
- error[invalid-return-type] sympy/matrices/matrixbase.py:147:27: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] sympy/matrices/matrixbase.py:151:27: Function always implicitly returns `None`, which is not assignable to return type `int`
- error[invalid-return-type] sympy/sets/sets.py:95:53: Function always implicitly returns `None`, which is not assignable to return type `Set`
- error[invalid-return-type] sympy/sets/sets.py:112:73: Function always implicitly returns `None`, which is not assignable to return type `Basic`
- error[invalid-return-type] sympy/sets/sets.py:120:70: Function always implicitly returns `None`, which is not assignable to return type `Set`
- Found 13508 diagnostics
+ Found 13409 diagnostics
No memory usage changes detected ✅

@MatthewMckee4 MatthewMckee4 marked this pull request as ready for review July 15, 2025 19:22
@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Jul 15, 2025
generator_functions: FxHashSet<FileScopeId>,

/// Set of all scopes that are inside `if TYPE_CHECKING` blocks.
function_scopes_in_type_checking: FxHashSet<FileScopeId>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's more memory efficient if we store this on Scope instead of having another hash map

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by that, sorry

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, add an attribute to the Scope struct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated now

Comment on lines 1516 to 1533
let if_block_in_type_checking =
if let ast::Expr::Name(ast::ExprName { id, .. }) = &*node.test {
id == "TYPE_CHECKING"
} else {
false
};

let else_elif_block_in_type_checking =
if let ast::Expr::UnaryOp(ast::ExprUnaryOp { op, operand, .. }) = &*node.test {
*op == ruff_python_ast::UnaryOp::Not
&& if let ast::Expr::Name(ast::ExprName { id, .. }) = &**operand {
id == "TYPE_CHECKING"
} else {
false
}
} else {
false
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm aware this pollutes this arm a bit, should I move this to a different function outwith the SemanticIndexBuilder struct?

@MatthewMckee4 MatthewMckee4 changed the title Support if TYPE_CHECKING [ty] Support empty function bodies in if TYPE_CHECKING blocks Jul 15, 2025
@github-actions
Copy link
Contributor

ecosystem-analyzer results

Lint rule Added Removed Changed
invalid-return-type 0 208 0
unused-ignore-comment 18 0 0
Total 18 208 0

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Let's add a few more tests for nested if statements because I think they aren't handled correctly yet.

Did you review the ecosystem results. do they look correct to you?

Comment on lines +2137 to +2139
if self.scope().scope(self.db()).in_type_checking_block() {
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unfortunate that we need to pre-compute this information in semantic index builder considering that empty function bodies in type checking blocks are rare. Ideally, we'd compute it lazily whether the function is inside a TYPE_CHECKING block but I don't see how we can do this easily because our AST doesn't allow upward traversal.

@sharkdp sharkdp removed their request for review July 16, 2025 07:49
MatthewMckee4 and others added 2 commits July 16, 2025 21:17
Co-authored-by: Carl Meyer <carl@oddbird.net>
@carljm carljm enabled auto-merge (squash) July 16, 2025 20:20
@carljm carljm dismissed MichaReiser’s stale review July 16, 2025 20:48

The issues identified were fixed

@carljm carljm merged commit cbe94b0 into astral-sh:main Jul 16, 2025
37 checks passed
@MatthewMckee4
Copy link
Contributor Author

Ah sorry let me revert that

@carljm
Copy link
Contributor

carljm commented Jul 16, 2025

It's all taken care of and merged already, thanks!

@MatthewMckee4
Copy link
Contributor Author

Ah, didn't see that on my phone, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ecosystem-analyzer ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[invalid-return-type] should not be emitted on function definitions inside if TYPE_CHECKING blocks

4 participants