Comments (6)
Actually, having a self argument with an Any
type should be accepted. This can be useful when adapting dynamically typed programs. However, the following are bad at least stylistically, and it would be useful to report them somewhow:
- explicit, non-Any type for the self argument
- missing self argument
Since the name of self is up to the programmer, we can't just complain if the first argument is not spelled 'self'. So this might be a good compromise: give a warning if the first argument of a method has an explicit type that is not Any
.
Now we can also diagnose some cases of forgetting to include self
, without having to assume that it is spelled self
:
class A:
def f(x: int) -> None: ... # Explicit type, not any => warning
This does not strictly have to be an error. For example, consider the above example: if the function is only called as A.f
the code could arguably be considered valid (though probably of questionable style).
from mypy.
@JukkaL: I can do this one.
from mypy.
Given the situation with #1586 I'm going to retag this as "needs discussion".
from mypy.
I wonder if we could do something like this:
- If the first arg is named
self
we assume it's going to be called as an instance method (assuming there's no@classmethod
or@staticmethod
) and the type of self should be compatible with the class (whatever that means) - If the first arg has another name, we're not sure; we allow any explicit type, but if there's no type we still assume it's an instance method (assuming the method is annotated at all)
- If the method is not annotated we assume nothing, except if
--check-untyped-defs
is used; then we use the same heuristic (whether the first arg is namedself
)
from mypy.
I think we can close this issue.
from mypy.
Whoops! Indeed.
from mypy.
Related Issues (20)
- Empty tuple type is expanded in truthy check instead of narrowed. HOT 3
- stubtest: `__all__` check seems to ignore `*.allowlist` files HOT 2
- cannot apply `@asynccontextmanager` to a function that uses `TypeVarTuple` HOT 2
- False positive: `Protocol member cannot be final` in non-instance assignment in a method HOT 3
- Variadic tuple unpacking requires a star target -- why? HOT 2
- 1.11 Release Tracking Issue HOT 8
- `Generator` return type and send type are treated as `None` under `allow-any-generics` HOT 3
- (false positive) Using `@final` decorator on a `Protocol` class results in an error in most cases HOT 3
- Descriptors that have different behavior on instance and class don't work with `no-strict-optional` HOT 1
- Simple `Literal` type narrowing via pattern matching is broken HOT 1
- return value of sorted() cannot be upcast HOT 1
- --follow-imports does not have an effect on following stub files HOT 1
- decorated classes trigger Incompatible types in "await" (actual type "DecoratedClass", expected type "Awaitable[Any]")
- `Attribute 'arguments' of 'FuncItem' undefined` in `get_type_analyze_hook` with caching HOT 1
- Regression on validation of manual type annotations to functools.partial between stable version and git HOT 2
- Generic class clone method using property variable for type arg produces name-defined error
- Literal pattern matching HOT 3
- False Positive error: "yield from" can't be applied to "Foo" [misc] where Foo has explicit __iter__(self) HOT 1
- False positive "Missing return statement" for coroutine result pattern matching HOT 1
- Infering return type as a union of all returns in type-checked functions HOT 5
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 mypy.