Title: Flag for disallowing Any · Issue #3470 · python/mypy · GitHub
Open Graph Title: Flag for disallowing Any · Issue #3470 · python/mypy
X Title: Flag for disallowing Any · Issue #3470 · python/mypy
Description: In general, we want to provide the user with a flag to disallow the use of implicit and explicit Any types as well as expressions with type Any, so that they can make sure all of their code is typechecked. @ddfisher and I did some brains...
Open Graph Description: In general, we want to provide the user with a flag to disallow the use of implicit and explicit Any types as well as expressions with type Any, so that they can make sure all of their code is type...
X Description: In general, we want to provide the user with a flag to disallow the use of implicit and explicit Any types as well as expressions with type Any, so that they can make sure all of their code is type...
Opengraph URL: https://github.com/python/mypy/issues/3470
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Flag for disallowing Any","articleBody":"In general, we want to provide the user with a flag to disallow the use of implicit and explicit Any types as well as expressions with type Any, so that they can make sure all of their code is typechecked.\r\n\r\n@ddfisher and I did some brainstorming yesterday and this proposal is what we came to.\r\nHere are all the places that `Any` can appear in, followed by discussion of the current state of the flags and the proposal for a new flag.\r\n\r\n# Types\r\n## Explicit\r\n\r\nA user can explicitly specify an `Any` type in variable definition (`x: Any = \"hello\"`), function definition(`def foo() → Any: ...`), a cast(`cast(Any, x)`), class definition (`class C(Any)`), `NewType`, a `TypeVar`, `NamedTuple`, or part of a type(`List[Any]`). \r\n\r\n## Implicit\r\n\r\n### Unimported\r\nUnimported types happen when we try to use a type from an import from a module we don’t analyze. This could happen if we’re using `--follow-imports=skip` or `--ignore-missing-imports`. \r\nConsider the following example\r\n```\r\n # unanalyzed.py\r\n class C:\r\n pass\r\n```\r\n```\r\n # test.py\r\n from unanalyzed import C\r\n \r\n def foo(c: C) -> str:\r\n return c\r\n```\r\nIf you run `mypy test.py --follow-imports=skip`, the code typechecks from mypy’s point of view. Because `C` is unresolved, it implicitly becomes an alias for `Any`. \r\n\r\n### Omitted Generics\r\nThis case occurs when the user does not explicitly specify a generic parameter for a type. For instance, `List` in a type position becomes `List[Any]`. It is easy to forget to specify a generic parameter. This can also happen with e.g. lower-case `list` from builtins.\r\n\r\nWe should consider disallowing omitting the generic parameter and maybe forbid using builtin types in favor of types from `typing` module. The proposal for it is [here](https://github.com/python/mypy/issues/3469).\r\n\r\n\r\n### Omitted Function Signature element\r\nWhen a user does not specify an explicit type for a parameter or a return type, it becomes `Any`. \r\nFor example, the function `def foo(x) → None: …`\r\nimplicitly becomes: `def foo(x: Any) → None: …`\r\n\r\n### From Error\r\n`Any` types can come from an ignored error. For instance:\r\n\r\n from typing import Union\r\n T = Union[UhOh] # type: ignore\r\n x: T\r\n reveal_type(x) # error: Revealed type is 'Any'\r\n\r\n### Expressions\r\n\r\nAn expression of type `Any` can come from any `Any` types described above. \r\nFor instance, `x + y` gets type `Any` when `x` has type `Any` even if `y` doesn't.\r\n\r\nIn real-life code, it would be challenging to achieve no `Any` expressions in the entire module. Note that if the user is calling an untyped method from a different module, expression can have an `Any` type, even if all `Any` types are disallowed in the current module. Still, users may want the ability to disallow expressions of type `Any` completely in certain circumstances.\r\n\r\n\r\n----------\r\n# Current State\r\n## Unimported types\r\n\r\nCurrently, there is a PR (https://github.com/python/mypy/pull/3405) that adds a flag to disallow unimported types. \r\n\r\n\r\n## Omitted Generics\r\n\r\nThere is currently a [PR](https://github.com/python/mypy/pull/3141) for a flag that would give a warning on omitted generic types. I believe we should at the very least have it on by default. Ideally, we should forbid omitted generics.\r\n\r\n\r\n## Omitted Function\r\n\r\nFlag `--disallow-untyped-defs` is already implemented in https://github.com/python/mypy/pull/1285. This functionality should be merged into the new flag.\r\n\r\n\r\n## From Error\r\n\r\nCurrently, there is no flag to disable it.\r\n\r\n## Expressions\r\n\r\nCurrently, there is no flag to disable it.\r\n\r\n----------\r\n# Flag\r\n\r\nAll of these are very similar in their nature and it would make sense to have one flag to rule them all. \r\nIt is generally worth it to provide a user with an option to disable each of the types of Anys.\r\nThere are several ways to do it.\r\n\r\n\r\n## Have a different flag for each type\r\n\r\nCurrently, in order to disable a type of Any, you have to use a separate flag for each type. At the very least we should name the flags with similar names because the options are very closely related to each other. \r\n\r\n## Have several “umbrella” flags\r\n\r\nAn option would be to have several flags that would cover different types. For instance, `--disallow-unimported-any-types`, `--disallow-ommited-function-types`, `--disallow-implicit-any-types`, `--disallow-explicit-any-types`, and `--disallow-all-any`.\r\nHowever, having this many flags is cumbersome, especially when they overlap. The documentation and usage would be confusing.\r\n\r\n\r\n## Have One Flag with List of Options\r\n\r\nI think the best option is to have one flag that takes a list of arguments.\r\nThis could look something like this:\r\n`--disallow-any=...` where `...` can be `all`, `unimported`, `omitted`,`error`, `expr`\r\nWe should also be able to combine several into one, like this: `--disallow-any=unimported,omitted`. This combination, for example, would disallow imported types and disallow omitted function types. \r\nIf the flag parameters are `all` + any other flag, we should give an error saying that it’s redundant and linking to documentation.\r\n","author":{"url":"https://github.com/ilinum","@type":"Person","name":"ilinum"},"datePublished":"2017-05-27T00:13:22Z","url":"https://github.com/python/mypy/issues/3470"}
| react-profiling | 0 |
| react-import-map | react,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-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:7f505ad3-80cf-8061-2375-06d97260b3e8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BFDE:3E1398:4E7AF1:6927C9:6B002F5C |
| html-safe-nonce | 0736a869495fd76f2976db7bf109e3ec64ee8729ec38cddf918a59310ac7ff3f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRkRFOjNFMTM5ODo0RTdBRjE6NjkyN0M5OjZCMDAyRjVDIiwidmlzaXRvcl9pZCI6Ijg1NDgxODc0Mzg2NjM2MTAyMDQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 93e29bdbe8b0e1309082fe366e75aa444819a1ff33a268511b540396fb664757 |
| hovercard-subject-tag | issue:231760293 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/3470/issue_layout |
| twitter:image | https://opengraph.githubassets.com/32e9c2b50304f9353c0ea2b97e4b407a5b50b34b9c103383ab4db470f5167140/python/mypy/issues/3470 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/32e9c2b50304f9353c0ea2b97e4b407a5b50b34b9c103383ab4db470f5167140/python/mypy/issues/3470 |
| og:image:alt | In general, we want to provide the user with a flag to disallow the use of implicit and explicit Any types as well as expressions with type Any, so that they can make sure all of their code is type... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ilinum |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width