Your implementation is incorrect for types other than list, set, and a few others because it assumes that any type parameter is the type you get when you iterate over something. But that’s not correct in general. For example, iterating over an Awaitable[int] doesn’t give you ints. The implementation also doesn’t look like it would handle dict[A, B] correctly.
These are fundamental problems that can’t be solved at runtime in general. Given a runtime object that is an instance of a generic type, you cannot in general figure out the type parameters that a static type checker would infer.