C# “is” operator – is that reflection?

Referencing ECMA-335, the is operator generates the isinst object model IL instruction (Partition III §4.6), which is part of the base instruction set as opposed to being part of the Reflection library (Partition IV §5.5). Edit: The is operator is extremely efficient compared to the reflection library. You could perform basically the same test much … Read more

what exactly is python typing.Callable?

typing.Callable is the type you use to indicate a callable. Most python types that support the () operator are of the type collections.abc.Callable. Examples include functions, classmethods, staticmethods, bound methods and lambdas. In summary, anything with a __call__ method (which is how () is implemented), is a callable. PEP 677 attempted to introduce implicit tuple-with-arrow … Read more

What is the correct way in python to annotate a path with type hints? [duplicate]

I assume that typical path objects are either Paths or strs, as such you could use a Union. In addition, the more inclusive os.PathLike is preferred over pathlib.Path. Python 3.10 or newer: import os def read_json(path: str | os.PathLike): … Python 3.5 – 3.9: import os import typing def read_json(path: typing.Union[str, os.PathLike]): …

TypedDict when keys have invalid names

According to PEP 589 you can use alternative syntax to create a TypedDict as follows: Movie = TypedDict(‘Movie’, {‘name’: str, ‘year’: int}) So, in your case, you could write: from typing import TypedDict RandomAlphabet = TypedDict(‘RandomAlphabet’, {‘A(2)’: str}) or for the second example: RandomAlphabet = TypedDict(‘RandomAlphabet’, {‘return’: str}) PEP 589 warns, though: This syntax doesn’t … Read more

Only allow specific components as children in React and Typescript

To do that you need to extract props interface from children component (and preferably also parent) and use it this way: interface ParentProps { children: ReactElement<ChildrenProps> | Array<ReactElement<ChildrenProps>>; } so in your case it would look like this: interface IMenu { children: ReactElement<IMenuItem> | Array<ReactElement<IMenuItem>>; } const MenuItem: React.FunctionComponent<IMenuItem> = ({ props }) => { … Read more

NoReturn vs. None in “void” functions – type annotations in Python 3.6

NoReturn means the function never returns a value. The function either does not terminate or always throws an exception: “The typing module provides a special type NoReturn to annotate functions that never return normally. For example, a function that unconditionally raises an exception..”. from typing import NoReturn def stop() -> NoReturn: raise RuntimeError(‘no way’) That … Read more

tech