René's URL Explorer Experiment


Title: Strict `Optional` checking · Issue #1450 · python/mypy · GitHub

Open Graph Title: Strict `Optional` checking · Issue #1450 · python/mypy

X Title: Strict `Optional` checking · Issue #1450 · python/mypy

Description: (copied from internal discussion) Proposal With one exception, make MyPy enforce optional semantics as specified in PEP 484: By default, None is an invalid value for any type, unless a default value of None has been provided in the funct...

Open Graph Description: (copied from internal discussion) Proposal With one exception, make MyPy enforce optional semantics as specified in PEP 484: By default, None is an invalid value for any type, unless a default valu...

X Description: (copied from internal discussion) Proposal With one exception, make MyPy enforce optional semantics as specified in PEP 484: By default, None is an invalid value for any type, unless a default valu...

Opengraph URL: https://github.com/python/mypy/issues/1450

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Strict `Optional` checking","articleBody":"(copied from internal discussion)\n## Proposal\n\nWith one exception, make MyPy enforce optional semantics as specified in PEP 484:\n\n> By default, None is an invalid value for any type, unless a default value of None has been provided in the function definition.\n> …\n> As a shorthand for Union[T1, None] you can write Optional[T1].\n\nThe one exception will be in the case of instance variables defined in the class body.  These variables may be assigned `None` while having a non-`Optional` type, provided that they are initialized to a non-`None` value by the end of `__init__`.  They may be initialized in `__init__` itself, or in any method on `self` that `__init__` calls, or in `__new__`.  Mypy will not check these variables for use before assignment to a non-`None` value in `__init__` or called methods, though it may make sense to add that at some point in the future.\nThe purpose of this exception is to allow variables to be typed in a common place rather than scattered around `__init__`.\n\nHere’s how this would look in particular:\n### Make `None` a real type\n\nIt supports a limited set of operations. `Optional[x]` is the same as `Union[x, None]`, and this is generally different from just `x`.\n\n``` python\n    def f(x: Optional[str]) -> None:\n        x.startswith('x')   # error, None has no startswith\n\n    def g(x: str) -> None: ...\n\n    g(None)  # error\n```\n### Infer `Optional[...]` from `None` default value\n\n``` python\n    def f(x: str = None) -> None:\n        # type of x is Optional[str]\n        ...\n\n    f('')   # ok\n    f(None) # ok\n```\n### If `[not] value` check\n\n``` python\n    def f(x: str = None) -> None:\n        if x:\n            # type of x should be str here\n            x.startswith('x')  # should be fine\n        else:\n            # type of x should be Optional[str] here\n            x.startswith('x')  # error\n```\n### If `value is [not] None` check\n\n``` python\n    def f(x: str = None) -> None:\n        if x is not None:\n            # type of x should be str here\n            x.startswith('x')  # should be fine\n        else:\n            # type of x should be None here\n            x.startswith('x')  # error\n```\n### `assert x`\n\n``` python\n    def f(x: str = None) -> None:\n        assert x\n        x.startswith('x')  # should be fine\n```\n\nSimilar for `assert [not] x`\n### or/and\n\n``` python\n    def f(x: str = None, y: str = None) -> None:\n        x and x.startswith('x')          # should be fine (and easy)\n        x is None or x.startswith('x')   # should be fine\n        x and y and x.startswith('x') and y.startswith('x')   # fine\n\n    def g(x: str = None) -> str:\n        return x or \"default\"\n```\n### Generators/comprehensions with condition check\n\n``` python\n    a = ... # type: List[Optional[int]]\n    b = [x for x in b if x]\n    # type of b should be List[int]\n```\n### Overloading with `None`\n\nThe new overload syntax (I can’t remember whether it’s in PEP 484 already) would allow overloading based on `None` values:\n\n``` python\n    @overload\n    def f(x: None) -> None: ...\n    @overload\n    def f(x: str) -> int: ...\n\n    def f(x):\n        if x:\n            return int(x)\n        else:\n            return None\n\n    f('')    # type is str\n    f(None)  # type is None\n```\n### Class Variable that Gets Initialized to `None`\n\n``` python\n    class A:\n        x = None  # type: str\n        def __init__(self) -> None:\n            x = \"value\"\n\n    class B:\n        x = None  # type: str\n        def __init__(self) -> None:\n            self.setup()\n\n        def setup(self) -> None:\n            x = \"value\"\n```\n### Attribute that Gets Initialized to `None`\n\n``` python\n    class A:\n        x = None  # type: str\n        def __init__(self) -> None:\n            self.x = None  # type: ignore\n\n        def load(self, f: IO[str]) -> None:\n            self.x = f.read()\n\n        def process(self) -> None:\n            print(x + 'x')  # should be fine?\n\n    class B:\n        def __init__(self) -> None:\n            self.x = None  # type: Optional[str]\n\n        def load(self, f: IO[str]) -> None:\n            self.x = f.read()\n\n        def process(self) -> None:\n            assert self.x    # needed, otherwise next line is an error\n            print(x + 'x')   # fine\n```\n## Future/Out of Scope Work for Initial Version\n### Functions without Explicit Return with Non-Optional Return Type\n\nWe’ll need to do some control flow analysis to handle cases like this, so we shouldn't implement this initially.\n\n``` python\n    def f(x: int) -> int:\n      if x > 0:\n        return 5\n      else:\n        pass  # uh oh, no return value here\n```\n## Development Plan\n\nWill probably need to start with #1278 as a first step (but not something to be merged separately).  Will be behind a feature flag initially and turned on by default later.\n","author":{"url":"https://github.com/ddfisher","@type":"Person","name":"ddfisher"},"datePublished":"2016-04-28T20:22:25Z","url":"https://github.com/python/mypy/issues/1450"}

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:4c00dd56-a53a-0b9b-9313-df9a6e6502f8
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idBB16:160FAB:38F63C:4FE34C:6B005E4B
html-safe-nonce131d004159bc3a2d368c3b3ccd5a071a317629b284d09e5608ea14383d43b983
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQjE2OjE2MEZBQjozOEY2M0M6NEZFMzRDOjZCMDA1RTRCIiwidmlzaXRvcl9pZCI6IjI3NDU1Nzc0NjA4Nzg4MjcwODMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac5ea8ae372d76441cf3b0680043509d7e7922dfd664752c83df91a0a4235d4850
hovercard-subject-tagissue:151734450
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/mypy/1450/issue_layout
twitter:imagehttps://opengraph.githubassets.com/44886e49377a244d64b4ff924dd562880a8513ecf8c77e7aa7f1fc9d8731ca9a/python/mypy/issues/1450
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/44886e49377a244d64b4ff924dd562880a8513ecf8c77e7aa7f1fc9d8731ca9a/python/mypy/issues/1450
og:image:alt(copied from internal discussion) Proposal With one exception, make MyPy enforce optional semantics as specified in PEP 484: By default, None is an invalid value for any type, unless a default valu...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameddfisher
hostnamegithub.com
expected-hostnamegithub.com
None29be975efff6507a5d135de9c7d634cf7ff8f0c55222a64386ab3202209aaf73
turbo-cache-controlno-preview
go-importgithub.com/python/mypy git https://github.com/python/mypy.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id7053637
octolytics-dimension-repository_nwopython/mypy
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id7053637
octolytics-dimension-repository_network_root_nwopython/mypy
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
releaseeacd927ba6112e3b01ba99c762bd699a4a4f5df7
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/mypy/issues/1450#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fmypy%2Fissues%2F1450
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%2Fmypy%2Fissues%2F1450
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%2Fmypy
Reloadhttps://github.com/python/mypy/issues/1450
Reloadhttps://github.com/python/mypy/issues/1450
Reloadhttps://github.com/python/mypy/issues/1450
pythonhttps://github.com/python
mypyhttps://github.com/python/mypy
Codehttps://github.com/python/mypy
Issues2.7k (2.7k)https://github.com/python/mypy/issues
Pull requests545 (545)https://github.com/python/mypy/pulls
Actionshttps://github.com/python/mypy/actions
Projectshttps://github.com/python/mypy/projects
Wikihttps://github.com/python/mypy/wiki
Security and qualityhttps://github.com/python/mypy/security
Insightshttps://github.com/python/mypy/insights
https://github.com/python/mypy
New issuehttps://github.com/login?return_to=https://github.com/python/mypy/issues/1450
New issuehttps://github.com/login?return_to=https://github.com/python/mypy/issues/1450
Strict Optional checkinghttps://github.com/python/mypy/issues/1450#top
https://github.com/ddfisher
featurehttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22feature%22
https://github.com/ddfisher
ddfisherhttps://github.com/ddfisher
on Apr 28, 2016https://github.com/python/mypy/issues/1450#issue-151734450
#1278https://github.com/python/mypy/issues/1278
gvanrossumhttps://github.com/gvanrossum
featurehttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22feature%22
on Apr 29, 2016https://github.com/python/mypy/issues/1450#event-645168407
ddfisherhttps://github.com/ddfisher
on May 4, 2016https://github.com/python/mypy/issues/1450#event-650871345
https://github.com/porglezomp
porglezomphttps://github.com/porglezomp
on May 6, 2016https://github.com/python/mypy/issues/1450#issuecomment-217550507
ddfisherhttps://github.com/ddfisher
completedhttps://github.com/python/mypy/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
cb1786fhttps://github.com/python/mypy/commit/cb1786fc81166cacd3a8f04436899b187225a81b
on Jun 10, 2016https://github.com/python/mypy/issues/1450#event-688976726
https://github.com/gvanrossum
gvanrossumhttps://github.com/gvanrossum
on Jun 11, 2016https://github.com/python/mypy/issues/1450#issuecomment-225322766
https://github.com/revmischa
revmischahttps://github.com/revmischa
on Dec 8, 2017https://github.com/python/mypy/issues/1450#issuecomment-350202247
https://github.com/emmatyping
emmatypinghttps://github.com/emmatyping
on Dec 8, 2017https://github.com/python/mypy/issues/1450#issuecomment-350209274
@revmischahttps://github.com/revmischa
https://mypy.readthedocs.io/en/latest/kinds_of_types.html#experimental-strict-optional-type-and-none-checkinghttps://mypy.readthedocs.io/en/latest/kinds_of_types.html#experimental-strict-optional-type-and-none-checking
https://github.com/revmischa
revmischahttps://github.com/revmischa
on Dec 8, 2017https://github.com/python/mypy/issues/1450#issuecomment-350209534
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/mypy/issues/1450
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/mypy/issues/1450
ddfisherhttps://github.com/ddfisher
featurehttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22feature%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.