René's URL Explorer Experiment


Title: Introduce `typing.STRICTER_STUBS` · Issue #1096 · python/typing · GitHub

Open Graph Title: Introduce `typing.STRICTER_STUBS` · Issue #1096 · python/typing

X Title: Introduce `typing.STRICTER_STUBS` · Issue #1096 · python/typing

Description: Some functions can return different types depending on passed arguments. For example: open(name, 'rb') returns io.BufferedReader, whereas open(name, 'wb') returns io.BufferedWriter. The typeshed accurately models this using @typing.overl...

Open Graph Description: Some functions can return different types depending on passed arguments. For example: open(name, 'rb') returns io.BufferedReader, whereas open(name, 'wb') returns io.BufferedWriter. The typeshed ac...

X Description: Some functions can return different types depending on passed arguments. For example: open(name, 'rb') returns io.BufferedReader, whereas open(name, 'wb') returns io.BufferedWriter....

Opengraph URL: https://github.com/python/typing/issues/1096

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Introduce `typing.STRICTER_STUBS`","articleBody":"Some functions can return different types depending on passed arguments. For example:\r\n* `open(name, 'rb')` returns `io.BufferedReader`, whereas\r\n* `open(name, 'wb')` returns `io.BufferedWriter`.\r\n\r\nThe typeshed accurately models this using `@typing.overload` and `typing.Literal`. However there is the case that the argument value deciding the return type cannot be determined statically, for example:\r\n\r\n```python\r\ndef my_open(name: str, write: bool):\r\n    with open(name, 'wb' if write else 'rb') as f:\r\n        content = f.read()\r\n```\r\n\r\nIn this case typeshed currently just claims that `open` returns `typing.IO[Any]`, so `content` ends up having the type `Any`, resulting in a loss of type safety (e.g. `content.startswith('hello')` will lead to a runtime error if the file was opened in binary mode, but type checkers won't be able to warn you about this because of `Any`).\r\n\r\nWhile typeshed could theoretically just change the return type to `typing.IO[Union[str, bytes]]`, that would force all existing code bases that currently rely on `Any` to type check to update their code, which is of course unacceptable.\r\n\r\nWhen starting a new project I however want the strictest type stubs possible. I explicitly do not want standard library functions to return unsafe values like `Any` (or the a bit less unsafe `AnyOf` suggested in #566), when the return types can be modeled by a `Union`.\r\n\r\nI therefore propose the introduction of a new variable `typing.STRICTER_STUBS: bool`, that's only available during type checking.\r\n\r\nWhich would allow typeshed to do the following:\r\n\r\n```python\r\nif typing.STRICTER_STUBS:\r\n    AnyOrUnion = typing.Union\r\nelse:\r\n    AnyOrUnion = typing.Any\r\n```\r\n\r\nAmbiguous return types could then be annotated as e.g. `-> typing.IO[AnyOrUnion[str, bytes]]`.\r\n\r\nThis would allow users to opt into stricter type stubs, if they so desire, without forcing changes on existing code bases.\r\n\r\nCC: @AlexWaygood, @JelleZijlstra, @srittau, @hauntsaninja, @rchen152, @erictraut\r\n\r\nP.S. Since I have seen `Union` return types being dismissed because \"the caller needs to use `isinstance()`\", I want to note that this is not true, if the caller wants to trade type safety for performance, they can always just add an explicit `Any` annotation to circumvent the runtime overhead of `isinstance`. Union return types force you to either handle potential type errors or explicitly opt out of type safety, which I find strongly preferable to lack of type safety by default.","author":{"url":"https://github.com/not-my-profile","@type":"Person","name":"not-my-profile"},"datePublished":"2022-03-01T16:28:06Z","url":"https://github.com/python/typing/issues/1096"}

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:ce259f84-da8f-dfec-6d21-9e9582e01d0c
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id8CAE:197995:21AE64B:2DB2871:6B00239D
html-safe-nonce4186e8c9cc3d9b8bdafdaef09445230bf512a127296ddba8307714ac881c705f
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4Q0FFOjE5Nzk5NToyMUFFNjRCOjJEQjI4NzE6NkIwMDIzOUQiLCJ2aXNpdG9yX2lkIjoiNzEwNzI5Mjg2NTEwMDc4NDU0MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmacd81b3817b38c88a77418dbbf4603384c3c88dcc3e7d8d2a8855b91cd2e7f2065
hovercard-subject-tagissue:1155547089
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/typing/1096/issue_layout
twitter:imagehttps://opengraph.githubassets.com/878420e58e1d774c2820c373316358578eabae0d97401028a05024b1fc2e1634/python/typing/issues/1096
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/878420e58e1d774c2820c373316358578eabae0d97401028a05024b1fc2e1634/python/typing/issues/1096
og:image:altSome functions can return different types depending on passed arguments. For example: open(name, 'rb') returns io.BufferedReader, whereas open(name, 'wb') returns io.BufferedWriter. The typeshed ac...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamenot-my-profile
hostnamegithub.com
expected-hostnamegithub.com
None4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0
turbo-cache-controlno-preview
go-importgithub.com/python/typing git https://github.com/python/typing.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id24604008
octolytics-dimension-repository_nwopython/typing
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id24604008
octolytics-dimension-repository_network_root_nwopython/typing
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release44c658b24abf00491fb7ae5f56d42c9f38cbeab2
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/typing/issues/1096#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftyping%2Fissues%2F1096
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftyping%2Fissues%2F1096
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Ftyping
Reloadhttps://github.com/python/typing/issues/1096
Reloadhttps://github.com/python/typing/issues/1096
Reloadhttps://github.com/python/typing/issues/1096
pythonhttps://github.com/python
typinghttps://github.com/python/typing
Codehttps://github.com/python/typing
Issues206 (206)https://github.com/python/typing/issues
Pull requests27 (27)https://github.com/python/typing/pulls
Discussionshttps://github.com/python/typing/discussions
Actionshttps://github.com/python/typing/actions
Security and qualityhttps://github.com/python/typing/security
Insightshttps://github.com/python/typing/insights
https://github.com/python/typing
New issuehttps://github.com/login?return_to=https://github.com/python/typing/issues/1096
New issuehttps://github.com/login?return_to=https://github.com/python/typing/issues/1096
Introduce typing.STRICTER_STUBShttps://github.com/python/typing/issues/1096#top
topic: featureDiscussions about new features for Python's type annotationshttps://github.com/python/typing/issues?q=state%3Aopen%20label%3A%22topic%3A%20feature%22
https://github.com/not-my-profile
not-my-profilehttps://github.com/not-my-profile
on Mar 1, 2022https://github.com/python/typing/issues/1096#issue-1155547089
#566https://github.com/python/typing/issues/566
@AlexWaygoodhttps://github.com/AlexWaygood
@JelleZijlstrahttps://github.com/JelleZijlstra
@srittauhttps://github.com/srittau
@hauntsaninjahttps://github.com/hauntsaninja
@rchen152https://github.com/rchen152
@erictrauthttps://github.com/erictraut
Nexthttps://github.com/python/typing/issues/1096?timeline_page=1
not-my-profilehttps://github.com/not-my-profile
topic: featureDiscussions about new features for Python's type annotationshttps://github.com/python/typing/issues?q=state%3Aopen%20label%3A%22topic%3A%20feature%22
on Mar 1, 2022https://github.com/python/typing/issues/1096#event-6163309094
https://github.com/AlexWaygood
AlexWaygoodhttps://github.com/AlexWaygood
on Mar 1, 2022https://github.com/python/typing/issues/1096#issuecomment-1055638620
not-my-profilehttps://github.com/not-my-profile
on Mar 1, 2022https://github.com/python/typing/issues/1096#event-6163517385
https://github.com/not-my-profile
not-my-profilehttps://github.com/not-my-profile
on Mar 1, 2022https://github.com/python/typing/issues/1096#issuecomment-1055657445
not-my-profilehttps://github.com/not-my-profile
on Mar 1, 2022https://github.com/python/typing/issues/1096#event-6163549181
https://github.com/erictraut
erictrauthttps://github.com/erictraut
on Mar 1, 2022https://github.com/python/typing/issues/1096#issuecomment-1055673065
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on Mar 1, 2022https://github.com/python/typing/issues/1096#issuecomment-1055771709
https://github.com/not-my-profile
not-my-profilehttps://github.com/not-my-profile
on Mar 1, 2022https://github.com/python/typing/issues/1096#issuecomment-1055781621
@gvanrossumhttps://github.com/gvanrossum
@erictrauthttps://github.com/erictraut
https://github.com/JelleZijlstra
JelleZijlstrahttps://github.com/JelleZijlstra
on Mar 1, 2022https://github.com/python/typing/issues/1096#issuecomment-1055791967
@not-my-profilehttps://github.com/not-my-profile
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on Mar 1, 2022https://github.com/python/typing/issues/1096#issuecomment-1055810477
@gvanrossumhttps://github.com/gvanrossum
@JelleZijlstrahttps://github.com/JelleZijlstra
@srittauhttps://github.com/srittau
buffering: inthttps://github.com/python/typeshed/blob/fd750bf065fc06c4e567439df8b1b142724ed441/stdlib/builtins.pyi#L1311
https://github.com/srittau
srittauhttps://github.com/srittau
on Mar 1, 2022https://github.com/python/typing/issues/1096#issuecomment-1055820126
@not-my-profilehttps://github.com/not-my-profile
#566https://github.com/python/typing/issues/566
https://github.com/not-my-profile
not-my-profilehttps://github.com/not-my-profile
on Mar 1, 2022https://github.com/python/typing/issues/1096#issuecomment-1055825468
python/typeshed#7416https://github.com/python/typeshed/pull/7416
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on Mar 1, 2022https://github.com/python/typing/issues/1096#issuecomment-1055825501
#566https://github.com/python/typing/issues/566
https://github.com/srittau
srittauhttps://github.com/srittau
on Mar 1, 2022https://github.com/python/typing/issues/1096#issuecomment-1055832621
@gvanrossumhttps://github.com/gvanrossum
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on Mar 1, 2022https://github.com/python/typing/issues/1096#issuecomment-1055839152
python/mypy#9275https://github.com/python/mypy/pull/9275
hauntsaninjahttps://github.com/hauntsaninja
on Mar 2, 2022https://github.com/python/typing/issues/1096#event-6174453421
Improve open overloads when mode is a literal unionhttps://github.com/hauntsaninja/typeshed/commit/e40e823c7095eb620a243f87f7deb463ce51c05a
e40e823https://github.com/hauntsaninja/typeshed/commit/e40e823c7095eb620a243f87f7deb463ce51c05a
Improve open overloads when mode is a literal unionhttps://github.com/hauntsaninja/typeshed/commit/43223af702d95a2cdf71cad016f87a05fbcd8c9e
43223afhttps://github.com/hauntsaninja/typeshed/commit/43223af702d95a2cdf71cad016f87a05fbcd8c9e
https://github.com/not-my-profile
not-my-profilehttps://github.com/not-my-profile
on Mar 4, 2022https://github.com/python/typing/issues/1096#issuecomment-1059138040
https://github.com/JelleZijlstra
JelleZijlstrahttps://github.com/JelleZijlstra
on Mar 4, 2022https://github.com/python/typing/issues/1096#issuecomment-1059330892
https://github.com/AlexWaygood
AlexWaygoodhttps://github.com/AlexWaygood
on Mar 4, 2022https://github.com/python/typing/issues/1096#issuecomment-1059334222
python/typeshed#7437https://github.com/python/typeshed/pull/7437
https://github.com/hmc-cs-mdrissi
hmc-cs-mdrissihttps://github.com/hmc-cs-mdrissi
on Mar 4, 2022https://github.com/python/typing/issues/1096#issuecomment-1059526253
commenthttps://github.com/python/typeshed/issues/4827#issuecomment-860263085
JelleZijlstrahttps://github.com/JelleZijlstra
on Mar 6, 2022https://github.com/python/typing/issues/1096#event-6191254821
Improve open overloads when mode is a literal union (#7428)https://github.com/python/typeshed/commit/9796b9ed69b3ec88cea773b9e50ad3c042431a8f
#7428https://github.com/python/typeshed/pull/7428
9796b9ehttps://github.com/python/typeshed/commit/9796b9ed69b3ec88cea773b9e50ad3c042431a8f
https://github.com/JukkaL
JukkaLhttps://github.com/JukkaL
on Mar 7, 2022https://github.com/python/typing/issues/1096#issuecomment-1060786773
https://github.com/erictraut
erictrauthttps://github.com/erictraut
on Mar 7, 2022https://github.com/python/typing/issues/1096#issuecomment-1061125141
https://github.com/JukkaL
JukkaLhttps://github.com/JukkaL
on Mar 8, 2022https://github.com/python/typing/issues/1096#issuecomment-1061730141
https://github.com/erictraut
erictrauthttps://github.com/erictraut
on Mar 8, 2022https://github.com/python/typing/issues/1096#issuecomment-1061836611
https://github.com/antonagestam
antonagestamhttps://github.com/antonagestam
on Mar 12, 2024https://github.com/python/typing/issues/1096#issuecomment-1992635735
https://github.com/antonagestam
antonagestamhttps://github.com/antonagestam
on Mar 12, 2024https://github.com/python/typing/issues/1096#issuecomment-1992637879
https://github.com/Polandia94
Polandia94https://github.com/Polandia94
on Aug 2, 2025https://github.com/python/typing/issues/1096#issuecomment-3146503157
@overloadhttps://github.com/overload
@overloadhttps://github.com/overload
@overloadhttps://github.com/overload
@overloadhttps://github.com/overload
https://github.com/flying-sheep
flying-sheephttps://github.com/flying-sheep
on Dec 26, 2025https://github.com/python/typing/issues/1096#issuecomment-3692941716
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/typing/issues/1096
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/typing/issues/1096
topic: featureDiscussions about new features for Python's type annotationshttps://github.com/python/typing/issues?q=state%3Aopen%20label%3A%22topic%3A%20feature%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.