How to avoid stubtest’s “[symbol] is not present at runtime” error

Example
Given the file ./mylib/minimalistic_repro.py

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Foo:
def __init__(self, param):
self.param = param
class Bar:
def __init__(self, param):
self.param = param
def foobar(par1, par2):
return par1.param + par2.param
</code>
<code>class Foo: def __init__(self, param): self.param = param class Bar: def __init__(self, param): self.param = param def foobar(par1, par2): return par1.param + par2.param </code>
class Foo:
    def __init__(self, param):
        self.param = param

class Bar:
    def __init__(self, param):
        self.param = param

def foobar(par1, par2):
    return par1.param + par2.param

and its stub ./mylib/minimalistic_repro.pyi

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from typing import TypeAlias
FooBar: TypeAlias = Foo | Bar
class Foo:
param: int
def __init__(self, param: int) -> None: ...
class Bar:
param: int
def __init__(self, param: int) -> None: ...
def foobar(par1: FooBar, par2: FooBar) -> int: ...
</code>
<code>from typing import TypeAlias FooBar: TypeAlias = Foo | Bar class Foo: param: int def __init__(self, param: int) -> None: ... class Bar: param: int def __init__(self, param: int) -> None: ... def foobar(par1: FooBar, par2: FooBar) -> int: ... </code>
from typing import TypeAlias

FooBar: TypeAlias = Foo | Bar

class Foo:
    param: int
    def __init__(self, param: int) -> None: ...

class Bar:
    param: int
    def __init__(self, param: int) -> None: ...

def foobar(par1: FooBar, par2: FooBar) -> int: ...

Note the type alias on the third line: FooBar: TypeAlias = Foo | Bar

mypy --strict mylib, flake8 mylib and ruff check --select E,F,B,SIM all pass.

When running stubtest however:
python -m mypy.stubtest mylib

I get the following error:
error: mylib.minimalistic_repro.FooBar is not present at runtime


My current workaround is to use an allowlist (stubtest --generate-allowlist).


Question(s)
● Is there a “better” way to avoid this “error”? / …
● … Am I doing something fundamentally wrong? …
● … and if not: Might this be worth a feature request?


Other approaches
● Of course I could declare
def foobar(par1: Foo | Bar, par2: Foo | Bar),
 but my actual task (writing type hints for a third party pypi package) requires a union of up to 18 types.

● I got the above example to run with stubtest by placing the FooBar type alias definition in a .py file (tp_aliases.py) and then reimporting. This approach failed to work in the case of with my actual pypi package type hinting task (tp_aliases.py is not part of the pypi package).

stubtest is complaining because it thinks your FooBar is a public API symbol, which might cause type checkers/IDE autocomplete to make incorrect assumptions and suggestions.

The “correct” way to fix it is to make it private; that is, precede the name with an underscore:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>_FooBar: TypeAlias = Foo | Bar
def foobar(par1: _FooBar, par2: _FooBar) -> int: ...
</code>
<code>_FooBar: TypeAlias = Foo | Bar def foobar(par1: _FooBar, par2: _FooBar) -> int: ... </code>
_FooBar: TypeAlias = Foo | Bar
def foobar(par1: _FooBar, par2: _FooBar) -> int: ...

For classes and functions, you can alternatively use typing.type_check_only:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># type_check_only is not available at runtime and can only be used in stubs
from typing import type_check_only
@type_check_only
def this_function_is_not_available_at_runtime() -> None: ...
@type_check_only
class AndSoDoesThisClass: ...
</code>
<code># type_check_only is not available at runtime and can only be used in stubs from typing import type_check_only @type_check_only def this_function_is_not_available_at_runtime() -> None: ... @type_check_only class AndSoDoesThisClass: ... </code>
# type_check_only is not available at runtime and can only be used in stubs
from typing import type_check_only

@type_check_only
def this_function_is_not_available_at_runtime() -> None: ...

@type_check_only
class AndSoDoesThisClass: ...

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật