-
-
Notifications
You must be signed in to change notification settings - Fork 140
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
Use explicit optional type on arguments #76
Conversation
Thanks for reporting. So far I was quite happy that I did not need to add On the other hand, I see how making that change would improve the experience when checking your code using In any case we should make the change either everywhere or nowhere. Currently I get 258 errors with As you suggested, we can consider using type aliases. One idea would be to use T = TypeVar('T')
Maybe = Optional[T]
class ExecutionResult(NamedTuple):
data: Maybe[Dict[str, Any]]
errors: Maybe[List[GraphQLError]] Or, perhaps: K = TypeVar('K')
V = TypeVar('V')
MaybeDict = Optional[Dict[K, V]]
MaybeList = Optional[List[V]]
class ExecutionResult(NamedTuple):
data: MaybeDict[str, Any]
errors: MaybeList[GraphQLError] We would spare 3 letters and be more similar to GraphQL.js on the one hand, but be less Pythonic on the other hand. I'm currently still a bit undecided. |
The problem with the type alias is that callers would need to import them to use the same signature. So probably impracticable and not a good idea. |
@Cito Long term plan is to remove |
@IvanGoncharov Thanks a lot for mentioning this. So it makes much less sense to use a |
Signed-off-by: Oleg Höfling <[email protected]>
Signed-off-by: Oleg Höfling <[email protected]>
@Cito I have added the |
The Zen of Python starts with "Beautiful is better than ugly." so we should keep it as it is. But then it continues "Explicit is better than implicit." so we should make that change. It's a real conflict of interests. But ok, well let's do it since you already put so much effort into this. We are following the Zen of Python anyway no matter what we do... |
@Cito thank you! I had another idea to shorten the from typing import Optional as Opt # or even O?
def execute(
schema: GraphQLSchema,
document: DocumentNode,
root_value: Any = None,
context_value: Any = None,
variable_values: Opt[Dict[str, Any]] = None,
operation_name: Opt[str] = None,
field_resolver: Opt[GraphQLFieldResolver] = None,
type_resolver: Opt[GraphQLTypeResolver] = None,
middleware: Opt[Middleware] = None,
execution_context_class: Opt[Type["ExecutionContext"]] = None,
) -> AwaitableOrValue[ExecutionResult]: |
Yes, I thought about that, but I'm not sure if that would really help, because people will not understand it immediately when viewing a function signature in an IDE and start to wonder whether |
This allows the type checks with
mypy
to pass in strict mode.The reason for this is that we run type checks with
mypy
using the--strict
mode which includesno_implicit_optional
. This means that calls like e.g.fill fail the check with an error
A possible workaround would be maintaining a private set of type stubs for the
graphql
package with optional types wrapped intoOptional
.If you find the suggestion useful, I can extend this to the whole codebase. Of course, I understand that e.g.
starts to loose readability, but maybe it's time to introduce type aliases for common args; I find the current API great and already useable to be declared stable.