Comments (10)
The Hack approach would basically be:
with typing.off:
...
The pylint approach has much more granularity but requires unique IDs to all checks.
from typing.
I like the simple with-statement. I think it is possible to write something that is both a decorator and a context manager. IIRC the unittest.mock package does this, so perhaps we could use the same name. When used as typing.off the cute name is fine; but after "from typing import off" it's too short IMO. (It turns off what?)
from typing.
The with statement is somewhat slow in CPython (the same applies to things like List[int]
and Undefined(...)
) and introduces an extra block nesting level, which makes code more difficult to read.
We could also have a comment that disables/enables all errors:
# typing: off
1 + 'x' # don't report an error
# typing: on
1 + 'x' # report an error
The comment should probably only extend until the end of the block that introduced it:
if foo:
# typing: off
1 + 'x' # don't report an error
1 + 'x' # report an error
from typing.
Hm. I'm not so keen on introducing more magic comments. See #35
from typing.
So we're introducing magic comments, but we might want to have something for stub modules to declare that a module exports absolutely anything: https://mail.python.org/pipermail/python-ideas/2015-January/031381.html
from typing.
Mypy has a related concept for classes as an enhancement proposal. It would allow any attribute that's not explicitly defined to be accessed with type Any
, but explicitly defined attributes could still have non-Any
types. Obviously this can be implemented by defining dummy __getattr__
and __setattr__
, but having special construct this may be less ugly and more explicit. For example:
from typing import Dynamic
class A(Dynamic):
x = 0
a = A()
a.x = '' # Error, x is an integer
x.y = '' # Okay, y is not explicitly defined so it defaults to Any
Here are more syntax ideas:
@dynamicclass
class A: ...
@openclass
class A: ...
class A:
* = Undefined(Any) # Based on Guido's idea
I like the idea of having something like this, both for modules and classes. It would make it easy to define partial, work-in-progress stubs with static types only for the most commonly used features, and everything else would default to Any
.
from typing.
There's probably a mypy task left here, and there's a TODO for the class decorator in my typing.py, but the decision is made so I'm closing this:
- To disable for the rest of the file:
# type: ignore
- To disable per class or method, use
@no_type_check
from typing.
The PEP draft is still somewhat unclear on this. Here's how I'd like # type: ignore
to work.
Case 1: ignore until end of block
if c:
# type: ignore
x.foo = 1 # don't complain about this
x.bar = 2 # potentially complain about this
Case 2: ignore until end of file (special case of 1)
x.foo = 1 # this is type checked
# type: ignore
x.bar = 2 # this is ignored by the type checker
...
Case 3: ignore single line (comment on a non-empty line)
x.foo = 2 # type: ignore # no complaints
x.bar = 3 # this is type checked
Case 4: ignore import statement
If we ignore an import statement, don't try to type check the imported module and don't complain if we can't find it. Give type Any
to any imported names:
from foo import x # type: ignore # should this be # type: Any?
import foo2 # type: ignore
x.bar() # okay, x has type Any
foo2.bar() # okay, foo2 has type Any
from typing.
So, is there still no way to ignore a small chunk of text? This would definitely work:
x = 1
if True:
# type: ignore
# this always runs and never gets type checked
x = 'abc'
but it feels ugly to me. I would like some kind of fake context manager:
with ignore_type:
...
but that would probably have speed issues.
from typing.
But how often do you really need that? Have you run into examples where you need this or are you just musing on potential use cases? The context manager feels too heavy to me (though if we want it, we could probably make the no_type_check decorator also act as a context manager, similar to some unittest decorators). Why couldn't you use x = 'abc' # type: Any
?
from typing.
Related Issues (20)
- hello
- Spec: Add PEP 696 (Type parameter defaults) HOT 2
- Spec: Add PEP 705 (Read-only TypedDict items) HOT 3
- Extra key of TypedDict HOT 2
- Reason given for disallowing non-concrete subtype assignment is unsound HOT 10
- Out-of-date description of how to indicate positional-only parameters
- Typing spec should be clearer that type checkers are not expected to support PEP 3141 HOT 22
- Incorporate PEP 483 into the spec HOT 5
- Conformance tests: Add mechanism to allow errors on one of multiple lines HOT 2
- Conformance tests: Add mechanism to ignore errors
- Conformance tests: Align automated and manual scoring HOT 1
- Introduce a `Language` type to provide consistent language information of strings. HOT 5
- Spec: Version and platform checking underspecified HOT 6
- Support list comprehension for `TypeVarTuple` HOT 2
- Clarify the float/int/complex special case HOT 5
- Clarify the implications of subclassing Any HOT 1
- Allow multiple `TypeVarTuple` in Generic Classes when they are wrapped. HOT 5
- pytype conformance tests crash if no `python3.11` executable is in `PATH` HOT 5
- Conformance test: make dataclass_hash.py not rely on `typing.Hashable`? HOT 1
- Proposal: Add coerced type narrowing similar to 'cast' HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typing.