-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
First tuple element type not being checked correctly #41
Comments
Hey @Yannik thanks for creating an issue. The way that type enforced works is a bit different than your expectations. In this case, you are checking that your output is either a tuple of list of strings or a tuple of list of integers. The comma operates as an Now to achieve exactly what you are looking for, you could always use a custom constraint to validate that you have a tuple of length 2 composed of lists. See: https://github.com/connor-makowski/type_enforced?tab=readme-ov-file#validate-with-constraints. Let me know if I can add any better clarity there. |
@connor-makowski Okay, got it. That's too bad! What I don't understand from response is why an error is thrown in the second case ( |
@connor-makowski I just tried to write a CustomConstraint for validating that the 2 values are returned from a function, the first one a list[str] and the second one a list[int]:
Is there any better way to do this? That's so hard to read :D |
I read some more on this topic, for example this part of the mypy docs: https://mypy.readthedocs.io/en/stable/kinds_of_types.html#tuple-types They say: And show the following example:
I found many more sources supporting that a However, if I understood you correctly, type_enforced handles this differently, so that This seems so confusing to me! Is there a special reason for this? |
@Yannik These are great comments. Let me address them appropriately. "What I don't understand from response is why an error is thrown in the second case (tuple[list[str],list[int]] instead of tuple[list[int],list[str]]). If it's just OR'ed, that shouldn't happen, should it?" The order does not matter here. I was referring to the fact that a tuple like Nice job with the custom constraint. Here is how I might do it:
tuple[a, b]: Back story: Type hints are loosely defined by python. Before python3.10, the EG: To allow multiple types as |
I think we still have a misunderstanding here. To me it seems like the order does matter. This does not throw an error:
This does:
The only difference here is the order of list[int] and list[str]. |
Understood.
I would expect the following behavior:
This is exactly how all static type checkers I know work (including mypy) and how I would usually expect type-hints to function, were they enforced by the language. Since we have |
@Yannik those are good points. Regarding different results based on order, I'll have to look into that. If so, I would expect a new minor version sometime soon. Regarding changing the way nested structures (like tuples of specific length) work, I want to look into that more. This would require a major version change with the package since it is a breaking change. I want to do some homework here before proceeding. I'm not sure on a timeframe for these changes. On a side note, it's great to have you using type_enforced. I designed it to easily enforce types in python webservers on the cloud. I open sourced it on pypi because it made server setup super easy. I am however curious as to why you are using it over mypy. Could you let me know why you are using this over another package? it would help me to better design it for other similar users. |
Makes sense! Thank you for looking into it.
Backstory: I am not a python programmer by trade, I mostly write one-file functional scripts or ansible modules to use in my infrastructure as code/devops work. The dynamically typed nature of python was never really a problem for this, however, for a bigger project I am currently working on (actually a web-api!) I wanted something more robust. |
Thanks for the backstory. This is helpful. I was able to identify the issue with two different nested types. EG:
To follow up on this, I am not entirely sure how I want to proceed here. This may take more time to solve than I was expecting. For internal use: This is related to how we build acceptable dicts of type values. See: https://github.com/connor-makowski/type_enforced/blob/main/type_enforced/enforcer.py#L87 The solution for checking multiple overlapping (collision) nested types as or statements cleanly is not immediately apparent. Current thinking on how to solve this require reworking the type checking data structure. This, however, would convolute the code and would substantially slow runtime speed. As a quick fix, consider firing a warning when this occurs. This should more appropriately be an error in the next major version if the larger issue is not fixed. |
Fixed in 1.6.0 |
I have the following code:
I would expect this to throw a TypeError. It however, does not throw an exception, but returns normally and prints
(['a'], ['b'])
.With method signature
def test() -> tuple[list[str],list[int]]:
(first element type is correct, second element type is incorrect), the TypeError is correctly thrown.I'm using type_enforced 1.5.0.
The text was updated successfully, but these errors were encountered: