Nullable return types in PHP7

PHP 7 introduces return type declarations. Which means I can now indicate the return value is a certain class, interface, array, callable or one of the newly hintable scalar types, as is possible for function parameters. function returnHello(): string { return ‘hello’; } Often it happens that a value is not always present, and that … Read more

Difference between defining typing.Dict and dict?

I am practicing using type hints in Python 3.5. One of my colleague uses typing.Dict: import typing def change_bandwidths(new_bandwidths: typing.Dict, user_id: int, user_name: str) -> bool: print(new_bandwidths, user_id, user_name) return False def my_change_bandwidths(new_bandwidths: dict, user_id: int, user_name: str) ->bool: print(new_bandwidths, user_id, user_name) return True def main(): my_id, my_name = 23, “Tiras” simple_dict = {“Hello”: “Moon”} … Read more

How to resolve “must be an instance of string, string given” prior to PHP 7?

Here is my code: function phpwtf(string $s) { echo “$s\n”; } phpwtf(“Type hinting is da bomb”); Which results in this error: Catchable fatal error: Argument 1 passed to phpwtf() must be an instance of string, string given It’s more than a little Orwellian to see PHP recognize and reject the desired type in the same … Read more

Type hinting a collection of a specified type

Using Python 3’s function annotations, it is possible to specify the type of items contained within a homogeneous list (or other collection) for the purpose of type hinting in PyCharm and other IDEs? A pseudo-python code example for a list of int: def my_func(l:list<int>): pass I know it’s possible using Docstring… def my_func(l): “”” :type … Read more

Python type hinting without cyclic imports

I’m trying to split my huge class into two; well, basically into the “main” class and a mixin with additional functions, like so: main.py file: import mymixin.py class Main(object, MyMixin): def func1(self, xxx): … mymixin.py file: class MyMixin(object): def func2(self: Main, xxx): # <— note the type hint … Now, while this works just fine, … Read more