Ref<PyType> (@NotNull PyUnionType type) { final Collection<PyType> members = type.getMembers(); if (members.size() == 2) { boolean foundNone = false; PyType optional = null; for (PyType member : members) { if (PyNoneType.INSTANCE.equals(member)) { foundNone = true; } else if (member != null) { optional = member; } } if (foundNone) { return Ref.create(optional); } } return null; }