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

Python: type-hinting a classmethod that returns an instance of the class, for a class that is inherited

For an example of how to apply Brian’s answer: with a TypeVar: from typing import TypeVar AnyA = TypeVar(“AnyA”, bound=”A”) class A: def __init__(self): print(“A”) self.hello = “hello” @classmethod def bobo(cls: type[AnyA]) -> AnyA: return cls() class B(A): def __init__(self): print(“B”) super().__init__() self.world = “world” reveal_type(B.bobo()) # B with Self: from typing import Self class … Read more

Automatically closing braces in Emacs?

There’s also ‘paredit. The cheat sheet shows you all the commands available. happen to like it better than the electric mode suggested in another answer. Though paredit does only apply to (), so it may not meed your needs. But, to be honest, there’s a bunch of packages surrounding parenthesis. The wiki has them all … 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

typing animated text

A simple approach: const elsTyper = document.querySelectorAll(“[data-typer]”); const rand = (min, max) => Math.floor(Math.random() * (max – min + 1) + min); const typer = (el) => { const text = el.dataset.typer; const tot = text.length; let ch = 0; (function typeIt() { if (ch > tot) return; el.textContent = text.substring(0, ch++); setTimeout(typeIt, rand(60, 300)); … Read more