How can I export function signatures with mypy?
up vote
2
down vote
favorite
I would like to see what function signatures mypy infers. Is there a way to export all of them for my package?
python mypy
add a comment |
up vote
2
down vote
favorite
I would like to see what function signatures mypy infers. Is there a way to export all of them for my package?
python mypy
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I would like to see what function signatures mypy infers. Is there a way to export all of them for my package?
python mypy
I would like to see what function signatures mypy infers. Is there a way to export all of them for my package?
python mypy
python mypy
asked yesterday
Martin Thoma
38.6k50278493
38.6k50278493
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Mypy does not attempt to infer function signatures. Instead, mypy (and all PEP-484 compliant type checkers) treat the function signatures themselves as the "source of truth" and conduct all type-checking based on the available signatures.
Specifically, once a function has been (manually) assigned type hints, mypy will use that information to a) perform type checks within the function and b) make sure any other typed functions are calling that function in a type-safe way.
If you omit type signatures from a method, mypy skip checking that function entirely. You can override this behavior by calling mypy with either the --strict
or --check-untyped-defs
flag. Once you do so, mypy will assume that the function's parameters and return are all of type Any
, the dynamic type.
If you are working on a sufficiently large codebase, it may be onerous to add type hints to all of your existing functions by hand. In that case, you can try:
Performing whole-program type inference to try and infer type hints for your functions, try using pytype instead. That said, keep in mind that pytype is still very much a work-in-progress. Whole-program type inference is a far more challenging problem to solve vs the more local type inference PEP 484 type-checkers perform.
Using Monkeytype or pyannotate -- these programs hook into your code at runtime and will try and infer types based on the runtime behavior of your code.
Both of these approaches should generate draft-quality type hints you can iterate on.
The mypy docs have more info about strategies for adopting mypy to larger codebases: https://mypy.readthedocs.io/en/latest/existing_code.html
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Mypy does not attempt to infer function signatures. Instead, mypy (and all PEP-484 compliant type checkers) treat the function signatures themselves as the "source of truth" and conduct all type-checking based on the available signatures.
Specifically, once a function has been (manually) assigned type hints, mypy will use that information to a) perform type checks within the function and b) make sure any other typed functions are calling that function in a type-safe way.
If you omit type signatures from a method, mypy skip checking that function entirely. You can override this behavior by calling mypy with either the --strict
or --check-untyped-defs
flag. Once you do so, mypy will assume that the function's parameters and return are all of type Any
, the dynamic type.
If you are working on a sufficiently large codebase, it may be onerous to add type hints to all of your existing functions by hand. In that case, you can try:
Performing whole-program type inference to try and infer type hints for your functions, try using pytype instead. That said, keep in mind that pytype is still very much a work-in-progress. Whole-program type inference is a far more challenging problem to solve vs the more local type inference PEP 484 type-checkers perform.
Using Monkeytype or pyannotate -- these programs hook into your code at runtime and will try and infer types based on the runtime behavior of your code.
Both of these approaches should generate draft-quality type hints you can iterate on.
The mypy docs have more info about strategies for adopting mypy to larger codebases: https://mypy.readthedocs.io/en/latest/existing_code.html
add a comment |
up vote
0
down vote
Mypy does not attempt to infer function signatures. Instead, mypy (and all PEP-484 compliant type checkers) treat the function signatures themselves as the "source of truth" and conduct all type-checking based on the available signatures.
Specifically, once a function has been (manually) assigned type hints, mypy will use that information to a) perform type checks within the function and b) make sure any other typed functions are calling that function in a type-safe way.
If you omit type signatures from a method, mypy skip checking that function entirely. You can override this behavior by calling mypy with either the --strict
or --check-untyped-defs
flag. Once you do so, mypy will assume that the function's parameters and return are all of type Any
, the dynamic type.
If you are working on a sufficiently large codebase, it may be onerous to add type hints to all of your existing functions by hand. In that case, you can try:
Performing whole-program type inference to try and infer type hints for your functions, try using pytype instead. That said, keep in mind that pytype is still very much a work-in-progress. Whole-program type inference is a far more challenging problem to solve vs the more local type inference PEP 484 type-checkers perform.
Using Monkeytype or pyannotate -- these programs hook into your code at runtime and will try and infer types based on the runtime behavior of your code.
Both of these approaches should generate draft-quality type hints you can iterate on.
The mypy docs have more info about strategies for adopting mypy to larger codebases: https://mypy.readthedocs.io/en/latest/existing_code.html
add a comment |
up vote
0
down vote
up vote
0
down vote
Mypy does not attempt to infer function signatures. Instead, mypy (and all PEP-484 compliant type checkers) treat the function signatures themselves as the "source of truth" and conduct all type-checking based on the available signatures.
Specifically, once a function has been (manually) assigned type hints, mypy will use that information to a) perform type checks within the function and b) make sure any other typed functions are calling that function in a type-safe way.
If you omit type signatures from a method, mypy skip checking that function entirely. You can override this behavior by calling mypy with either the --strict
or --check-untyped-defs
flag. Once you do so, mypy will assume that the function's parameters and return are all of type Any
, the dynamic type.
If you are working on a sufficiently large codebase, it may be onerous to add type hints to all of your existing functions by hand. In that case, you can try:
Performing whole-program type inference to try and infer type hints for your functions, try using pytype instead. That said, keep in mind that pytype is still very much a work-in-progress. Whole-program type inference is a far more challenging problem to solve vs the more local type inference PEP 484 type-checkers perform.
Using Monkeytype or pyannotate -- these programs hook into your code at runtime and will try and infer types based on the runtime behavior of your code.
Both of these approaches should generate draft-quality type hints you can iterate on.
The mypy docs have more info about strategies for adopting mypy to larger codebases: https://mypy.readthedocs.io/en/latest/existing_code.html
Mypy does not attempt to infer function signatures. Instead, mypy (and all PEP-484 compliant type checkers) treat the function signatures themselves as the "source of truth" and conduct all type-checking based on the available signatures.
Specifically, once a function has been (manually) assigned type hints, mypy will use that information to a) perform type checks within the function and b) make sure any other typed functions are calling that function in a type-safe way.
If you omit type signatures from a method, mypy skip checking that function entirely. You can override this behavior by calling mypy with either the --strict
or --check-untyped-defs
flag. Once you do so, mypy will assume that the function's parameters and return are all of type Any
, the dynamic type.
If you are working on a sufficiently large codebase, it may be onerous to add type hints to all of your existing functions by hand. In that case, you can try:
Performing whole-program type inference to try and infer type hints for your functions, try using pytype instead. That said, keep in mind that pytype is still very much a work-in-progress. Whole-program type inference is a far more challenging problem to solve vs the more local type inference PEP 484 type-checkers perform.
Using Monkeytype or pyannotate -- these programs hook into your code at runtime and will try and infer types based on the runtime behavior of your code.
Both of these approaches should generate draft-quality type hints you can iterate on.
The mypy docs have more info about strategies for adopting mypy to larger codebases: https://mypy.readthedocs.io/en/latest/existing_code.html
answered 1 hour ago
Michael0x2a
21.3k1671124
21.3k1671124
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238088%2fhow-can-i-export-function-signatures-with-mypy%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password