Comments (12)
I can confirm this is a bug. Note that it is OOP specific. If not in classes, the same scenario appears to work as expected:
def log1(a: Any, b: Any = None) -> None:
print(a, b)
def log2(a, b: Any = None) -> None:
print(a, b)
if __name__ == '__main__':
typechecked('__main__')
log1(1)
log2(2)
Thanks for spotting this. However I am currently not able to work on pytypes, so this won't be fixed anytime soon (unless someone steps in and makes a PR).
from pytypes.
Another thing occurred to me which should be tracked here:
Todo: Check if this also affects @staticmethod
, @classmethod
and @property
.
from pytypes.
The cause is somewhere in type_util._funcsigtypes
.
from pytypes.
The issue is that type_util._handle_defaults
is not self
-aware.
from pytypes.
I think I fixed it on my local machine, but writing tests etc properly takes time. It would be helpful if you could rewrite your code sample above as a test (including @staticmethod
etc cases).
However until this is merged, let me point out a workaround:
You can turn off default-inference by
pytypes.infer_default_value_types = False
I checked that this "cures" the issue.
from pytypes.
Is it enough to have tests of using @typechecked
to decorate the various kinds of methods, or would you also need tests of calling it on a module (as in the code I originally reported), and/or tests of the underscored functions in type_util
?
from pytypes.
First of all, apologize that the test suite is such a mess. I know it needs refactoring but there are always other priorities.
The test for this should go next to https://github.com/Stewori/pytypes/blob/master/tests/test_typechecker.py#L4454 but as a separate test method. Link this issue like it is frequently done, e.g. in https://github.com/Stewori/pytypes/blob/master/tests/test_typechecker.py#L4762.
Python 3 syntax i.e. the helper class in this case, must go into https://github.com/Stewori/pytypes/blob/master/tests/testhelpers/typechecker_testhelper_py3.py, because test_typechecker still contains Python 2 tests (Python 2 support is kept for Jython).
I think there should only one class be required for this test.
Is it enough to have tests of using
@typechecked
to decorate the various kinds of methods
Yes. Ideally, simply decorate the helperclass with @typechecked
, that will apply to all methods.
Testing of the _ underscore methods is not required (I know it should be but this is not the time to revolutionize the test suite, see #11 ). For now I am fine with just a single symptomatical test that pins this issue, close to your initial example + staticmethod and classmethod cases if they should be affected by this issue (didn't check yet). (With "pin" I mean the test code should fail before the fix and pass after the fix. You can check this using pytypes.infer_default_value_types = False
)
If you prefer it, you can also just post the test code here and I'll put it into the right places. OTOH if you file a proper PR you will be credited in the project git history.
from pytypes.
No problem, the only difficulty I had with the test suite was that I didn't leave enough blank lines after my helper class, which caused a completely unrelated test to fail. Stupid comment-based annotations ;-)
@classmethod
and @property.setter
are affected, @staticmethod
isn't (probably no suprises there). Shall I put a staticmethod test in there anyway, for completeness, or do you prefer to have only the tests for this issue?
from pytypes.
Re testing staticmethod. I am undecided. Do it like you prefer.
from pytypes.
PR build fails on Travis in the way I expected (Python 2.7 and 3.4 pass because the tests are skipped. Python 3.5+ three tests fail). You have the permissions to make the fix in my fork before merging, if you prefer.
from pytypes.
While I'm in here, are you interested in PRs for either of the following minor niggles? I won't be offended if you aren't!
- Typo in the readme: "It's main features are currently" should have no apostrophe.
- Quite a bit of trailing whitespace in both the files I edited. I only noticed because I have my text editor set to trim trailing whitespace on save, which created a messy diff.
from pytypes.
Thanks for the help! Please go ahead.
from pytypes.
Related Issues (20)
- Utility to validate stubfile signatures against .py implementations HOT 4
- pytypes is_of_type: counter-intuitive result on semi-primitive types like list and dict HOT 5
- is_of_type does not work as expected with Annotated[Generic[T], ...] HOT 1
- Some differences between Python 3.6 and 3.8 HOT 3
- New release HOT 1
- please improve error messages when functions decorated with @typechecked are called with incorrect types
- pytypes breaks @autoreload in a somewhat dangerous way HOT 8
- `is_subtype` fails with `List[TypeVar]`
- Infer return type of generic functions based on input
- Support Python 3.10 HOT 7
- @typechecked fails to correctly parse functions with PEP 3102
- Incorrect collections imports in type_util.py HOT 1
- VSCode incorrect decorated func hint HOT 7
- AttributeError: 'NoneType' object has no attribute '__bound__' HOT 5
- Error in @typechecked with List[Dict[str, str]] HOT 1
- Remove backports.typing usage
- _check_caller_type can be broken by removing a blank line in typechecker_testhelper_py3.py
- typechecked(string) should have more consistent return type HOT 1
- Python 3.8 Compatibility 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 pytypes.